Topics

6/recent/ticker-posts

Sorting in Data Structures (Part - 1)


 Sorting in Data Structures

Note: Here the Sorting has been divided into types where the entire page is not enough so it is divided into two parts.

Sorting: A sorting algorithm is a process of rearranging the given array or list element values according to the comparison operator of the given elements. Here the comparison operator is used to set the order of the element in the respective data structure.


Based on the sorting algorithm sorting are majorly divided into five major kinds they are:
        1. bubble sorting
        2. Selection Sorting
        3. insertion Sorting
        4. Quick Sorting   
        5.  Merge Sorting

1. Bubble sorting: 
                                
                Bubble sorting is a simple algorithm where we will compare the adjacent elements and swap if the elements are not in a proper order. Remember, that this algorithm is not suitable for large data. 

Program for Bubble sorting: 

        #include <stdio.h>
        #include <conio.h>
        int main()
    {
        int a[100], n, i, j, s;
        printf("Enter number of elements\n");
        scanf("%d", &n);
        printf("Enter %d integers\n", n);
        for (i = 0; i < n; i++)
        scanf("%d", &a[i]);
        for (i= 0; i <= n-1; i++)
        {
        for (j = 0 ; j <= (n -i+ 1); j++)
        {
        if (a[j] > a[j+1])
        {    
        s= a[j];
        a[j] = a[j+1];
        a[j+1] = s;
        }
        }
        printf("Sorted list in ascending order:\n");
        for ( i = 0 ; i < n ; i++ )
        printf("%d\n", a[i]);
        getch();
    }
        

    2. Selection Sorting:
        
            Selection sorting algorithm is a algorithm where we will arrange the elements in a particular order either ascending or descending and here in this particular algorithm the elements from the first are compared with all the elements and if the number is found small then it will be swapped and made into the next element. 

Some steps for Selection Sorting are as follows: 

    Step 1: Select only the first element in the given list of elements 
    Step 2: Now compare selected element with all other elements given 
    Step 3: In between comparison any number that is found small then the data is swapped 
    Step 4: Now start comparing al the other elements in the same method 


Program for Selection Sorting:

        #include<stdio.h>
        #include<conio.h>
        void main()
        {
            int i,j,temp,n,a[20];
            printf("Enter the number of elements:");
            scanf("%d",&n);
            printf("\nEnter the elements:");
            for(i=0;i<n;i++)
            scanf("%d",&a[i]);
            for(i=0;i<n ;i++) {
            for(j=i+1;j<n ;j++) {
            if(a[i]>a[j])
          {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
          }
            }
            }
            printf("\nElements after sorting:");
            for(i=0;i<n;i++)
            printf("\n%d",a[i]);
            getch();
        }


3.Insertion Sorting: 

            Insertion sorting is also an algorithm which will sort either by arranging elements in ascending or descending order and it will check all the iterations and move each element from unsorted portion to sorted portion. 

Some steps for Insertion Sorting is as follows: 

    Step 1: Assume that the element chosen is sorted or checked and remaining all other are unsorted and to be checked.
    Step 2: Now check by taking each unsorted element and drop in sorted element to compare it. 
    Step 3: Now repeat the same process for all the elements in that program or array or list

Program for Insertion Sorting: 

        #include<stdio.h>
        void main()
        {
            int i,j,t,a[10],n,p=0;
            printf("enter the range of array:");
            scanf("%d",&n);
            printf("enter elements into array:");
            for(i=0;i<n;i++)
            scanf("%d",&a[i]);
            for(i=1;i<n;i++)
            {
                t=a[i];
                for(p=i;p>0 && a[p-1]>t;p--)
                a[p]=a[p-1];
                a[p]=t;
            }
            printf("the sorted order is:");
            for(i=0;i<n;i++)
            printf("\t%d",a[i]);
            getch();
         }

 

Note: For any Doubts related to this sorting please comment in  comment section.

Post a Comment

0 Comments