Advertisement
Guest User

a

a guest
Jan 21st, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3.  
  4. void quick_sort(int arr[20],int,int);
  5.  
  6. void main()
  7. {
  8.  int arr[20],n,i;
  9.  clrscr();
  10.  printf("Enter the number of elements in the Array: ");
  11.  scanf("%d",&n);
  12.  printf("\nEnter %d elements:\n\n",n);
  13.  
  14.  for(i=0 ; i<n ; i++)
  15.  {
  16.   printf(" Array[%d] = ",i);
  17.   scanf("%d",&arr[i]);
  18.  }
  19.  
  20.  quick_sort(arr,0,n-1);
  21.  printf("\nThe Sorted Array is:\n\n");
  22.  
  23.  for(i=0 ; i<n ; i++)
  24.  {
  25.   printf(" %4d",arr[i]);
  26.  }
  27.  getch();
  28. }
  29.  
  30. void quick_sort(int arr[20],int low,int high)
  31. {
  32.  int pivot,j,temp,i;
  33.  if(low<high)
  34.  {
  35.   pivot = low;
  36.   i = low;
  37.   j = high;
  38.  
  39.   while(i<j)
  40.   {
  41.    while((arr[i]<=arr[pivot])&&(i<high))
  42.    {
  43.     i++;
  44.    }
  45.  
  46.    while(arr[j]>arr[pivot])
  47.    {
  48.     j--;
  49.    }
  50.  
  51.    if(i<j)
  52.    {
  53.     temp=arr[i];
  54.     arr[i]=arr[j];
  55.     arr[j]=temp;
  56.    }
  57.   }
  58.  
  59.   temp=arr[pivot];
  60.   arr[pivot]=arr[j];
  61.   arr[j]=temp;
  62.   quick_sort(arr,low,j-1);
  63.   quick_sort(arr,j+1,high);
  64.  }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement