1#include<stdio.h>
2void quicksort(int number[25],int first,int last){
3 int i, j, pivot, temp;
4
5 if(first<last){
6 pivot=first;
7 i=first;
8 j=last;
9
10 while(i<j){
11 while(number[i]<=number[pivot]&&i<last)
12 i++;
13 while(number[j]>number[pivot])
14 j--;
15 if(i<j){
16 temp=number[i];
17 number[i]=number[j];
18 number[j]=temp;
19 }
20 }
21
22 temp=number[pivot];
23 number[pivot]=number[j];
24 number[j]=temp;
25 quicksort(number,first,j-1);
26 quicksort(number,j+1,last);
27
28 }
29}
30
31int main(){
32 int i, count, number[25];
33
34 printf("How many elements are u going to enter?: ");
35 scanf("%d",&count);
36
37 printf("Enter %d elements: ", count);
38 for(i=0;i<count;i++)
39 scanf("%d",&number[i]);
40
41 quicksort(number,0,count-1);
42
43 printf("Order of Sorted elements: ");
44 for(i=0;i<count;i++)
45 printf(" %d",number[i]);
46
47 return 0;
48}
49
1def partition(a,l,h):
2 pivot = a[l]
3 i = l
4 j=h
5 while i<j:
6 while a[i]<=pivot and i<h: i+=1
7 while a[j]>pivot and j>l: j-=1
8 if i<j: a[i],a[j]=a[j],a[i]
9
10 a[j],a[l]=a[l],a[j]
11 return j
12
13def quickSort(a,l,h):
14 if l < h:
15 pi = partition(a, l, h)
16 quickSort(a, l, pi - 1)
17 quickSort(a, pi + 1, h)
18
19#driver Code
20a =[10, 7, 8, 9, 1, 5 ]
21quickSort(a, 0, len(a) - 1)
22print(a)
23#Output: [1, 5, 7, 8, 9, 10]
1// @see https://www.youtube.com/watch?v=es2T6KY45cA&vl=en
2// @see https://www.youtube.com/watch?v=aXXWXz5rF64
3// @see https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html
4
5function partition(list, start, end) {
6 const pivot = list[end];
7 let i = start;
8 for (let j = start; j < end; j += 1) {
9 if (list[j] <= pivot) {
10 [list[j], list[i]] = [list[i], list[j]];
11 i++;
12 }
13 }
14 [list[i], list[end]] = [list[end], list[i]];
15 return i;
16}
17
18function quicksort(list, start = 0, end = undefined) {
19 if (end === undefined) {
20 end = list.length - 1;
21 }
22 if (start < end) {
23 const p = partition(list, start, end);
24 quicksort(list, start, p - 1);
25 quicksort(list, p + 1, end);
26 }
27 return list;
28}
29
30quicksort([5, 4, 2, 6, 10, 8, 7, 1, 0]);
31
1//last element selected as pivot
2#include <iostream>
3
4using namespace std;
5void swap(int*,int*);
6int partition(int arr[],int start,int end)
7{
8 int pivot=arr[end];
9 int index=start;
10 int i=start;
11 while(i<end)
12 {
13 if(arr[i]<pivot)
14 {
15 swap(&arr[index],&arr[i]);
16 index++;
17 }
18 i++;
19 }
20 swap(&arr[end],&arr[index]);
21 return index;
22}
23void quicksort(int arr[],int start,int end)
24{
25 if(start<end)
26 {
27 int pindex=partition(arr,start,end);
28 quicksort(arr,start,pindex-1);
29 quicksort(arr,pindex+1,end);
30 }
31}
32void display(int arr[],int n)
33{
34 for(int i=0;i<n;i++)
35 {
36 cout<<arr[i]<<" ";
37 }
38 cout<<endl;
39}
40
41int main()
42{
43 int n;
44 cout<<"enter the size of the array:"<<endl;
45 cin>>n;
46 int arr[n];
47 cout<<"enter the elements of the array:"<<endl;
48 for(int i=0;i<n;i++)
49 {
50 cin>>arr[i];
51 }
52 cout<<"sorted array is:"<<endl;
53 quicksort(arr,0,n-1);
54 display(arr,n);
55
56 return 0;
57}
58void swap(int *a,int*b)
59{
60 int temp=*a;
61 *a=*b;
62 *b=temp;
63}
64
1// A full c++ quicksort algorithm no bs
2// quicksort in code
3
4#include <iostream>
5
6using namespace std;
7
8void QuickSort(int arr[], int start, int end);
9int Partition(int arr[], int start, int end);
10void SwapArrMem(int arr[], int a, int b);
11
12int main()
13{
14
15 int arr[4]; //change the size of the array to your desired array size
16
17 cout << "enter " << sizeof(arr) / sizeof(arr[0]) << " numbers. press enter after input" << endl;
18
19 for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
20 {
21
22 cin >> arr[i];
23 }
24
25 cout << endl << "The sorted numbers are:" << endl << endl;
26
27
28
29 QuickSort(arr, 0, sizeof(arr) / sizeof(arr[0]) - 1);
30
31 for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
32 {
33 cout << arr[i] << endl;
34 }
35
36}
37
38void QuickSort(int arr[], int start, int end)
39{
40 if (start >= end) return;
41
42 int index = Partition(arr, start, end);
43 QuickSort(arr, start, index - 1);
44 QuickSort(arr, index + 1, end);
45}
46
47int Partition(int arr[], int start, int end)
48{
49 int pivotindex = start;
50 int pivotvalue = arr[end];
51 for (int i = start; i < end; i++)
52 {
53 if (arr[i] < pivotvalue)
54 {
55 SwapArrMem(arr, i, pivotindex);
56 pivotindex++;
57 }
58 }
59 SwapArrMem(arr, pivotindex, end);
60 return pivotindex;
61}
62
63void SwapArrMem(int arr[], int a, int b)
64{
65 int temp = arr[a];
66 arr[a] = arr[b];
67 arr[b] = temp;
68}