Tuesday, 22 January 2013
Simple Sorting Algorithm Using DMA
This post provides the source code for simple and naive integer sorting algorithm by exploiting the dynamic memory allocation feature of C programming.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *arr, i, j, n;
printf("Enter the number of items: ");
scanf("%d", &n);
arr = malloc(n * sizeof(int));
for (i = 0; i < n; i++)
{
printf("Enter the %dth item: ", i + 1);
scanf("%d", &arr[i]);
}
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - 1; j++)
{
int temp;
if (arr[j] > arr[j + 1])
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printf("The sorted array is:\n");
for (i = 0; i < n; i++)
{
printf("%d\n", arr[i]);
}
return 0;
}
samar@samar-Techgaun:~$ gcc -Wall -o sort sort.c samar@samar-Techgaun:~$ ./sort Enter the number of items: 5 Enter the 1th item: 2 Enter the 2th item: 4 Enter the 3th item: 1 Enter the 4th item: 92 Enter the 5th item: 45 The sorted array is: 1 2 4 45 92
Labels:
beginner,
c,
C/C++,
programming
Bookmark this post:blogger tutorials
Social Bookmarking Blogger Widget |
Simple Sorting Algorithm Using DMA
2013-01-22T16:54:00+05:45
Cool Samar
beginner|c|C/C++|programming|
Subscribe to:
Post Comments (Atom)