quick sort program in c

Solutions on MaxInterview for quick sort program in c by the best coders in the world

showing results for - "quick sort program in c"
Victoria
04 Sep 2018
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
Ignacio
03 Nov 2019
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}
Francisco
10 Feb 2020
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} 
Paulina
31 Jan 2018
1int cmpfunc (const void * a, const void * b) {
2   return ( *(int*)a - *(int*)b );
3}
queries leading to this page
quicksort in codequicksort sort c 2b 2bquick sort using cquick sort code in c with explanationquicksort with number of elementsquicksort code c 2b 2bquick sorting in data structurewrite a c program to sort the given set of numbers by performing partition 28 29 function using the divide and conquer strategyquicksort algorithm c 2b 2bquick c compilerexample array for quick sort to testhow to write quicksort algorithmarray quick sortways to implement quicksortc quick sortquick sort array in cquicksort 28int 5b 5d 29quick sort in data structurequick sort parametersquicksort library function in cquick sort using for loopquicksort array in cquicksort programmquick sort codequick sort code in c programmingquick sort implementation in carray quick sort c 2b 2bquicksort program in cc code for quick sortquicksort algorithm c simulationmodify the following in place quicksort implementation of code to a randomized version of algorithmquicksort explainedquick sort using recursion in cquick sorting with pthread code in cwrite an algorithm for quick sort and explain time complexity of quick sort with example quicksort algorithm in c codepartition sort in cquick sort easy implementationwhat is the best case time complexity of quicksortquicksort in c 2b 2bwrite a c program to implement quick sort algorithmquick sort n cquicksort sort cquick sort algorithm in cquicksort explained with examplepartition quicksort cquick sort easy codequick sort algorithm with pivotaverage complexity of quicksortsort an array using quick sort in cdescribe quicksort algorithmpartition algorithmwrite a c program to implement quick sort algorithm code for quicksortwrite a program to sort given set of numbers in ascending order using quick sort also print the number of comparison required to sort the given array quick sort algorithm geeksforgeeksquick sort code in cquick sort in c examplequick sort in cppcode of quick sortquick sort program in c 2b 2bquicksort codequicksort programquick sort program in c codegreperwhat will be the array after 2 pass in quick sortsource code of quicksort with mid element as pivotquick sort algorutm cc programming for quick sortpartition algorithm for quicksortquicksort 28a 29 algorithmquciksort cquick sort program in c using partitionquicksort pivot sortquicksort comes under which of the following 3fquick sort in c algorithmquicksort c implementationquick sort using divide and conquer stratergy 3fquicksort in data structurec program sort the given number in ascending order quick sortquick sort of array in cstable quicksort cexplain quicksorthow to divide the unsorted array in c languagequick sort explanation in cexplain quick sort code in c 29 how does the quicksort technique work 3f give c function for the same c array quick sortquicksort java examplequick sort using setquick sort code cquicksort algorithm explainedpivot sorting algorithmquick sort in c cormenwhat is quick sort in cquick sort time complexityquicksort an array in cquicksort time complexityquicksort with pivot as last elementquicksort tutorialquick sort c functionquicksort in placequicksort in c codequic sortquicksort space complexity analysisdivide and conquer in quick sortquick sort c programif 28temp 3d 3d1 29 quick sortfunction for quick sort in cquocksortquicksor tin cquick sort using median as pivotquick sort program in cquick sort algorithm in c step by stepwrite a c program to sort a list of elements using the quicksort algorithmquick sort algorithm c 2b 2bquicksort cquicksort c 2b 2bquick sort c implementationquicksort c examplepartition in quicksorttime complexity of quick sortquicksort c 2b 2b code example quick sort c codequick sorthow does quicksort work 3fquick sort algorithmquicksort in c libraryquick sorting codethe given list of number of list is to be sorted using quick sort 2c what is the complexityquick sort sorted arrayquicksort algorithm demoquick sort cquicksorting alogorith in cquicksort diagrampartition in quicksort c 2b 2bquicksort in place examplequicksort worst casequicksort with last element as pivot exampleprogram for quick sorting algorithmquick sort using structure array in cquick sort algorithm with an example how is an integer array sorted in place using the quicksort algorithm 3fquicksort c codewrite a e2 80 98c e2 80 99 function to sort an array using quick sort technique as per given declarations void quick sort 28int 5b 5d 2cint 29 3b 2f 2f first parameter is an array and second parameter is number of elements in place quicksortquicksort decresing program in cquick sort function in cquicksort cppquick sort i arrayhow to calculate time complexity of quick sortquicksort analysis complexityc 2b 2b quick sort algorithmquick sort recursive in cquicksort programizquick sort c 2b 2b codehow does quicksort workquicksort implementationquick sort program in c with time complexityquicksort sort algorithm in cquicksort pass wise output coddequicksort code solutionis quick sort divide and conquerquick sort of algorithmcode for quick sortwhen will quicksort workc 2b 2b quicksortshort quick sort codequicksort algorithmpivot element us taken in following sortingquick sort in cc quicksort programquick sort array c5 10 15 1 2 7 8 quicksortquick sort time complexity in cquick sort in c programquiclsort in ccode for partition funtion in cquick sort an array in cquick sort algorithm example in cquick sort algorithm examplequick sort to return a particular number in arrayin place quick sort algorithmquick sort code exampleimplement quick sort in cquick sort algorithmexample of quick sortquicksort complexityquicksort function in cquick sort algorithm small examplequicksort passwise outputimplementation of quicksort in cquick sort algorithm csimple quick sort program in cquick sorting cis quick sort in placequick sort using structure in cquicksort algorithm in cquick sort i csort function in c for arrayquicksort example 5cwhat is quick sortquick sort example program in cquick soryquicksort algorithm examples codequick sort program in c with code explanationquick sort program in c in one functionwhat is best case 2c worst case and average case complexity of quick sort 3fimplement quick sort using c19 29 write a program to implement quick sort using array as a data structure does quicksort use dynaic programmingquicksort c programquicksort functionquick sort codequicksort wikipediawrite a program to sort the list using quick sort 28using function 29how quicksort worksquick sort cppquick sort using loopsquick sort code in cquick sort pseudocode algorithmquicksort explained in cquicksort c 2b 2b codec quicksortsort the word using quicksortquick sort easy program in cquick sort start and end cquicksort in placequicksort iin cquick sort in c using temppuort element in quick sortquicksort code passwise outputsnew quicksort 28 29simple quick sort codequicksort operation iin cquick sort algorithm using compare in cquick sort in ascending order in cwhat is quicksortquicksort algorithm explanationquick sort in c 2b 2bpseudocode for quick sort considering first element as pivot in cquick sort descending order javaquick sort codequicksort in cquicksort algorithm cimplementation of quick sort in cfunction quicksort 28nums 29 7b 2f 2f write quick sort code here 7dquicksort code in cquick sort trong cc quicksort codehyk sort c languagequick sort dynamic programmingquick sort in c for sorting numbersquick sort arraywrite the quicksort quick srt in javapass this temporary array to both the quicksort function and the partition functionquicksort codigoquicksort code explainedquick sort algorithm cquick sort inc cquick sort code in cwrite a program to quicksort in cquick sort c program codequicksort partitionc program for quick sortquick sort algorithm example in c with structuresquick sort in cquicksort examplequick sort program in c