CPP 11
###### Quicksort.cpp Guest on 5th April 2021 10:52:03 AM
1. /*
2.  * quicksort.cpp
3.  *
4.  *  Created on: 25 Aug
5.  *      Author: lucascordeiro
6.  */
7.
8. // quickSort.c
9. #include <iostream>
10.
11. using namespace std;
12.
13.
14. void quick_sort(int arr[20],int,int);
15.
16. int main()
17. {
18.  int arr[]={1,5,10,2,3},n=5,i;
19.
20.  for(i=0 ; i<n ; i++)
21.   printf(" Array[%d] = %d\n",i,arr[i]);
22.
23.  quick_sort(arr,0,n-1);
24.
25.  printf("\nThe Sorted Array is:\n\n");
26.
27.  for(i=0 ; i<n ; i++)
28.  {
29.   printf(" %4d",arr[i]);
30.  }
31. }
32.
33. void quick_sort(int arr[20],int low,int high)
34. {
35.  int pivot,j,temp,i;
36.  if(low<high)
37.  {
38.   pivot = low;
39.   i = low;
40.   j = high;
41.
42.   while(i<j)
43.   {
44.    while((arr[i]<=arr[pivot])&&(i<high))
45.    {
46.     i++;
47.    }
48.
49.    while(arr[j]>arr[pivot])
50.    {
51.     j--;
52.    }
53.
54.    if(i<j)
55.    {
56.     temp=arr[i];
57.     arr[i]=arr[j];
58.     arr[j]=temp;
59.    }
60.   }
61.
62.   temp=arr[pivot];
63.   arr[pivot]=arr[j];
64.   arr[j]=temp;
65.   quick_sort(arr,low,j-1);
66.   quick_sort(arr,j+1,high);
67.  }
68. }

Paste-bin is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Recent Pastes

Raw Paste

or to edit or fork this paste. It's free.