Tuesday, October 28, 2014

C Array



Sort an Integer Array using LS DRadix Sort Algorithm
 
#include 
#include  
int min = 0, count = 0, array[100] = {0}, array1[100] = {0};
void main()
{
    int k, i, j, temp, t, n;
    clrscr();
    printf("Enter size of array :");
    scanf("%d", &count);
    printf("Enter elements into array :");
    for (i = 0; i < count; i++)
    {
        scanf("%d", &array[i]);
        array1[i] = array[i];
    }
    for (k = 0; k < 3; k++)
    {    
        for (i = 0; i < count; i++)
        {
            min = array[i] % 10;        
            t = i;
            for (j = i + 1; j < count; j++)    
            {
                if (min > (array[j] % 10))
                {
                    min = array[j] % 10; 
                    t = j;
                }
            }
            temp = array1[t];
            array1[t] = array1[i];
            array1[i] = temp;
            temp = array[t];
            array[t] = array[i];
            array[i] = temp;
        }
        for (j = 0; j < count; j++)        
                   array[j] = array[j] / 10;
    }
    printf("Sorted Array (lSdradix sort) : ");
    for (i = 0; i < count; i++)
        printf("%d ", array1[i]);
}

Output
 
Enter size of array :7
Enter elements into array :170    
45 
90
75
802
24
2
Sorted Array (ladradix sort) : 2 24 45 75 90 170 802 
Enter size of array :7    
Enter elements into array :22
64
121
78
159
206
348
Sorted Array (ladradix sort) : 22 64 78 159 121 206 348
Enter size of array :7
Enter elements into array :985
27
64
129
345
325
091
Sorted Array (ladradix sort) : 27 64 91 129 325 345 985
 
Sort an Array based on Heap Sort Algorithm
 
#include 
#include 
void main()
{
   int heap[10], no, i, j, c, root, temp;
    printf("\n Enter no of elements :");
    scanf("%d", &no);
    printf("\n Enter the nos : ");
    for (i = 0; i < no; i++)
       scanf("%d", &heap[i]);
    for (i = 1; i < no; i++)
    {
        c = i;
        do
        {
            root = (c - 1) / 2;             
            if (heap[root] < heap[c])   
            {
                temp = heap[root];
                heap[root] = heap[c];
                heap[c] = temp;
            }
            c = root;
        } while (c != 0);
    }
 
    printf("Heap array : ");
    for (i = 0; i < no; i++)
        printf("%d\t ", heap[i]);
    for (j = no - 1; j >= 0; j--)
    {
        temp = heap[0];
        heap[0] = heap[j    
        heap[j] = temp;
        root = 0;
        do 
        {
            c = 2 * root + 1;    
            if ((heap[c] < heap[c + 1]) && c < j-1)
                c++;
            if (heap[root]<heap[c] && c<j)    
            {
                temp = heap[root];
                heap[root] = heap[c];
                heap[c] = temp;
            }
            root = c;
        } while (c < j);
    } 
    printf("\n The sorted array is : ");
    for (i = 0; i < no; i++)
       printf("\t %d", heap[i]);
    printf("\n Complexity : \n Best case = Avg case = Worst case = O(n logn) \n");
}
 
Output
 
Average case 
Enter no of elements :7
Enter the nos : 6
5
3
1
8
7
2
Heap array : 8   6       7       1       5       3       2
The sorted array is :      1     2     3     5     6     7     8
Complexity : 
Best case = Avg case = Worst case = O(n logn) 
Enter no of elements :7
Enter the nos : 12
10
8
9
7
4
2
Heap array : 12  10      8       9       7       4       2
The sorted array is :      2     4     7     8     9     10     12
Complexity : 
Best case = Avg case = Worst case = O(n logn) 
            Enter no of elements :7
Enter the nos : 5
7
12
6
9
10
14
Heap array : 14  9    12      5       6       7       10
The sorted array is :  5     6     7     9     10     12     14
Complexity : 
Best case = Avg case = Worst case = O(n logn) 
 
Sort the Array Elements using Gnome Sort
 
#include 
#include 
void main()
{
    int i, temp, ar[10], n;
    printf("\n Enter the elemts number u would like to enter:");
    scanf("%d", &n);
    printf("\n Enter the elements to be sorted through gnome sort:\n");
    for (i = 0; i < n; i++)
        scanf("%d", &ar[i]);
    i = 0;
    while (i < n)
    {
        if (i == 0 || ar[i - 1] <= ar[i])
            i++;
        else
        {
            temp = ar[i-1];
            ar[i - 1] = ar[i];
            ar[i] = temp;
            i = i - 1;
        }
    }
    for (i = 0;i < n;i++)
        printf("%d\t", ar[i]);
}
 
Output
 
Enter the elemts number u would like to enter:7
Enter the elements to be sorted through gnome sort:
6
0
9
5
2
4
3
0       2       3       4       5       6       9       
 
Enter the elemts number u would like to enter:6
Enter the elements to be sorted through gnome sort:
1
2
4
5
6
7
1       2       4       5       6       7       
 
Enter the elemts number u would like to enter:9
Enter the elements to be sorted through gnome sort:
9
8
7
6
5
4
3
3
2
2       3       3       4       5       6       7       8       9

No comments:

Post a Comment