fork download
  1. #include<stdio.h>
  2.  
  3. void swap (int arr[], int left, int right)
  4. {
  5. int temp;
  6. temp=arr[left];
  7. arr[left]=arr[right];
  8. arr[right]=temp;
  9. }
  10. void quicksort( int arr[], int low, int high )
  11. {
  12. int pivot;
  13. if ( high > low )
  14. {
  15. pivot = partition( arr, low, high );
  16. quicksort( arr, low, pivot-1 );
  17. quicksort( arr, pivot+1, high );
  18. }
  19. }
  20. int partition( int arr[], int low, int high )
  21. {
  22. int left, right;
  23. int item_pivot;
  24. int pivot = left = low;
  25. item_pivot = arr[low];
  26. right = high;
  27. while ( left < right )
  28. {
  29. while( arr[left] <= item_pivot )
  30. left++;
  31. while( arr[right] > item_pivot)
  32. right--;
  33. if ( left < right )
  34. swap(arr,left,right);
  35. }
  36. arr[low] = arr[right];
  37. arr[right] = item_pivot;
  38. return right;
  39. }
  40. void printarray(int arr[], int);
  41.  
  42. int main()
  43. {
  44. int arr[50], i, n;
  45. //printf("\nEnter no. of elements: ");
  46. scanf("%d", &n);
  47. //printf("\nEnter the elements: \n");
  48. for (i=0; i<n; i++)
  49. scanf ("%d", &arr[i]);
  50. printf("\nUnsorted array: \n");
  51. printarray(arr,n);
  52. quicksort(arr,0,n-1);
  53. printf("\nSorted array: \n");
  54. printarray(arr,n);
  55. }
  56. void printarray(int arr[], int n)
  57. {
  58. int i;
  59. for (i=0; i<n; i++)
  60. printf(" %d ", arr[i]);
  61. printf("\n");
  62. }
Success #stdin #stdout 0s 2252KB
stdin
10
7
44
2
10
1
35
74
88
5
3
15
stdout
Unsorted array: 
 7  44  2  10  1  35  74  88  5  3 

Sorted array: 
 1  2  3  5  7  10  35  44  74  88