Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.93 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4. //quick Sort function to Sort Integer array list
  5. void quicksort(int array[], int firstIndex, int lastIndex)
  6. {
  7.     //declaaring index variables
  8.     int pivotIndex, temp, index1, index2;
  9.  
  10.     if(firstIndex < lastIndex)
  11.     {
  12.         //assigninh first element index as pivot element
  13.         pivotIndex = firstIndex;
  14.         index1 = firstIndex;
  15.         index2 = lastIndex;
  16.  
  17.         //Sorting in Ascending order with quick sort
  18.         while(index1 < index2)
  19.         {
  20.             while(array[index1] <= array[pivotIndex] && index1 < lastIndex)
  21.             {
  22.                 index1++;
  23.             }
  24.             while(array[index2]>array[pivotIndex])
  25.             {
  26.                 index2--;
  27.             }
  28.  
  29.             if(index1<index2)
  30.             {
  31.                 //Swapping opertation
  32.                 temp = array[index1];
  33.                 array[index1] = array[index2];
  34.                 array[index2] = temp;
  35.             }
  36.         }
  37.  
  38.         //At the end of first iteration, swap pivot element with index2 element
  39.         temp = array[pivotIndex];
  40.         array[pivotIndex] = array[index2];
  41.         array[index2] = temp;
  42.  
  43.         //Recursive call for quick sort, with partiontioning
  44.         quicksort(array, firstIndex, index2-1);
  45.         quicksort(array, index2+1, lastIndex);
  46.     }
  47. }
  48.  
  49. int main()
  50. {
  51.     //Declaring variables
  52.     int array[100],n,i;
  53.  
  54.     //Number of elements in array form user input
  55.     printf("Enter the number of element you want to Sort : ");
  56.     scanf("%d",&n);
  57.  
  58.     //code to ask to enter elements from user equal to n
  59.     printf("Enter Elements in the list : ");
  60.     for(i = 0; i < n; i++)
  61.     {
  62.         scanf("%d",&array[i]);
  63.     }
  64.  
  65.     //calling quickSort function defined above
  66.     quicksort(array,0,n-1);
  67.  
  68.     //print sorted array
  69.     printf("Sorted elements: ");
  70.     for(i=0;i<n;i++)
  71.     printf(" %d",array[i]);
  72.  
  73.     getch();
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement