Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:49:13

0001 
0002 // name=quicksort ; gcc $name.cc -std=c++11 -lstdc++ -o /tmp/$name && /tmp/$name
0003 //  https://hackr.io/blog/quick-sort-in-c
0004 
0005 #include<stdio.h>
0006 
0007 void quicksort(int number[25],int first,int last)
0008 {
0009     int i, j, pivot, temp;
0010 
0011     if(first<last)
0012     {
0013 
0014         pivot=first;
0015 
0016         i=first;
0017 
0018         j=last;
0019 
0020         while(i<j)
0021         {
0022             while(number[i]<=number[pivot] && i < last) i++;
0023 
0024             while(number[j]>number[pivot]) j--;
0025 
0026             if(i<j)
0027             {
0028                 temp=number[i];
0029                 number[i]=number[j];
0030                 number[j]=temp;
0031             }
0032 
0033         }
0034 
0035         temp=number[pivot];
0036 
0037         number[pivot]=number[j];
0038 
0039         number[j]=temp;
0040 
0041         quicksort(number,first,j-1);
0042 
0043         quicksort(number,j+1,last);
0044     }
0045 }
0046 
0047 int main(){
0048 
0049     int i, count, number[25];
0050 
0051     printf("Enter some elements (Max. - 25): ");
0052 
0053     scanf("%d",&count);
0054 
0055     printf("Enter %d elements: ", count);
0056 
0057     for(i=0;i<count;i++)
0058 
0059     scanf("%d",&number[i]);
0060 
0061     quicksort(number,0,count-1);
0062 
0063     printf("The Sorted Order is: ");
0064 
0065     for(i=0;i<count;i++)
0066 
0067     printf(" %d",number[i]);
0068 
0069     return 0;
0070 
0071 }