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
1// Quick sort in C
2
3#include <stdio.h>
4
5// function to swap elements
6void swap(int *a, int *b) {
7 int t = *a;
8 *a = *b;
9 *b = t;
10}
11
12// function to find the partition position
13int partition(int array[], int low, int high) {
14
15 // select the rightmost element as pivot
16 int pivot = array[high];
17
18 // pointer for greater element
19 int i = (low - 1);
20
21 // traverse each element of the array
22 // compare them with the pivot
23 for (int j = low; j < high; j++) {
24 if (array[j] <= pivot) {
25
26 // if element smaller than pivot is found
27 // swap it with the greater element pointed by i
28 i++;
29
30 // swap element at i with element at j
31 swap(&array[i], &array[j]);
32 }
33 }
34
35 // swap the pivot element with the greater element at i
36 swap(&array[i + 1], &array[high]);
37
38 // return the partition point
39 return (i + 1);
40}
41
42void quickSort(int array[], int low, int high) {
43 if (low < high) {
44
45 // find the pivot element such that
46 // elements smaller than pivot are on left of pivot
47 // elements greater than pivot are on right of pivot
48 int pi = partition(array, low, high);
49
50 // recursive call on the left of pivot
51 quickSort(array, low, pi - 1);
52
53 // recursive call on the right of pivot
54 quickSort(array, pi + 1, high);
55 }
56}
57
58// function to print array elements
59void printArray(int array[], int size) {
60 for (int i = 0; i < size; ++i) {
61 printf("%d ", array[i]);
62 }
63 printf("\n");
64}
65
66// main function
67int main() {
68 int data[] = {8, 7, 2, 1, 0, 9, 6};
69
70 int n = sizeof(data) / sizeof(data[0]);
71
72 printf("Unsorted Array\n");
73 printArray(data, n);
74
75 // perform quicksort on data
76 quickSort(data, 0, n - 1);
77
78 printf("Sorted array in ascending order: \n");
79 printArray(data, n);
80}
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}
1int cmpfunc (const void * a, const void * b) {
2 return ( *(int*)a - *(int*)b );
3}