quick sort

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

showing results for - "quick sort"
Roberta
21 Jan 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
Alan
26 Apr 2018
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
Rébecca
02 Jul 2017
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
Andrés
19 Apr 2020
1#include<stdio.h>
2int partition(int arr[], int low, int high) {
3  int temp;
4  int pivot = arr[high];
5  int i = (low - 1); 
6  for (int j = low; j <= high - 1; j++) {
7    if (arr[j] <= pivot) { 
8      i++; 
9      temp = arr[i];
10      arr[i] = arr[j];
11      arr[j] = temp;
12    } 
13  } 
14  temp = arr[i + 1];
15  arr[i + 1] = arr[high];
16  arr[high] = temp;
17  return (i + 1); 
18} 
19void quick_sort(int arr[], int low, int high) { 
20  if (low < high) {
21    int pi = partition(arr, low, high); 
22    quick_sort(arr, low, pi - 1); 
23    quick_sort(arr, pi + 1, high); 
24  } 
25} 
26int print(int arr[], int n) {
27  for(int i = 0; i < n; i++) {
28    printf("%d ", arr[i]);
29  }
30}
31
32int main()
33{
34int n, i;
35scanf("%d", &n);
36int arr[n];
37for(i = 0; i < n; i++)
38{
39scanf("%d", &arr[i]);
40}
41quick_sort(arr, 0, n - 1);
42print(arr, n);
43}
Constance
20 Jan 2021
1//I Love Java
2import java.io.*;
3import java.util.*;
4import java.util.stream.*;
5import static java.util.Collections.*;
6
7import static java.util.stream.Collectors.*;
8
9public class Quick_Sort_P {
10
11    static void swap(List<Integer> arr, int i, int j) {
12        int temp = arr.get(i);
13        arr.set(i, arr.get(j));
14        arr.set(j, temp);
15    }
16
17    static int partition(List<Integer> arr, int low, int high) {
18
19        int pivot = arr.get(high);
20        int i = (low - 1);
21
22        for (int j = low; j <= high - 1; j++) {
23
24            if (arr.get(j) < pivot) {
25
26                i++;
27                swap(arr, i, j);
28            }
29        }
30        swap(arr, i + 1, high);
31        return (i + 1);
32    }
33
34    static void quickSort(List<Integer> arr, int low, int high) {
35        if (low < high) {
36
37            int pi = partition(arr, low, high);
38
39            quickSort(arr, low, pi - 1);
40            quickSort(arr, pi + 1, high);
41        }
42    }
43
44    public static void main(String[] args) throws IOException {
45
46        BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
47
48        List<Integer> arr = Stream.of(buffer.readLine().replaceAll("\\s+$", "").split(" ")).map(Integer::parseInt)
49                .collect(toList());
50
51        int n = arr.size();
52
53        quickSort(arr, 0, n - 1);
54        System.out.println("Sorted array: ");
55        System.out.println(arr);
56    }
57}
Elina
03 Jan 2018
1void swap(int* a, int* b)
2{
3    int t = *a;
4    *a = *b;
5    *b = t;
6}
7 
8/* This function takes last element as pivot, places
9the pivot element at its correct position in sorted
10array, and places all smaller (smaller than pivot)
11to left of pivot and all greater elements to right
12of pivot */
13int partition (int arr[], int low, int high)
14{
15    int pivot = arr[high]; // pivot
16    int i = (low - 1); // Index of smaller element and indicates the right position of pivot found so far
17 
18    for (int j = low; j <= high - 1; j++)
19    {
20        // If current element is smaller than the pivot
21        if (arr[j] < pivot)
22        {
23            i++; // increment index of smaller element
24            swap(&arr[i], &arr[j]);
25        }
26    }
27    swap(&arr[i + 1], &arr[high]);
28    return (i + 1);
29}
30 
31/* The main function that implements QuickSort
32arr[] --> Array to be sorted,
33low --> Starting index,
34high --> Ending index */
35void quickSort(int arr[], int low, int high)
36{
37    if (low < high)
38    {
39        /* pi is partitioning index, arr[p] is now
40        at right place */
41        int pi = partition(arr, low, high);
42 
43        // Separately sort elements before
44        // partition and after partition
45        quickSort(arr, low, pi - 1);
46        quickSort(arr, pi + 1, high);
47    }
48}
49 
50/* Function to print an array */
51void printArray(int arr[], int size)
52{
53    int i;
54    for (i = 0; i < size; i++)
55        cout << arr[i] << " ";
56    cout << endl;
57}
58 
59// Driver Code
60int main()
61{
62    int arr[] = {10, 7, 8, 9, 1, 5};
63    int n = sizeof(arr) / sizeof(arr[0]);
64    quickSort(arr, 0, n - 1);
65    cout << "Sorted array: \n";
66    printArray(arr, n);
67    return 0;
68}
similar questions
quick sort in cquicksort
queries leading to this page
best time and worst time complexity for quick sort algorithmquicksort expartition sort nquick sort n cquick sort code in cquicksort algorithm runtimequicksort aloc program for quick sortquicksort worst case time complexityquicksort wikiquicksort time complexityquocksortquick sort use casequick sort c implementationquick sort code in c programmingquicksort codequicksort definitionwhat is best case worst case and average case for quick sortquicksort code passwise outputscode for quick sortquicksort c exampleif 28temp 3d 3d1 29 quick sortwhat is the big o of quicksorttime complexity of quick sort in worst case time complexity of quick sort in worst and best case using master theorem quick sort code in cquick sort codetime complexity graph of quicksortbest case complexity of quick sorthow does a quicksort workhow does quicksort work 3fquick sort average runtimequick sort using divide and conquer stratergy 3fquick sort algorithm 27quick sort start and end cquick sort algorithmbest case time complexity quick sortquick sort codequick sort in c algorithmquick sort using cquick sort code cfunction quicksort 28nums 29 7b 2f 2f write quick sort code here 7dwhy is quicksort conquer trivialquicksort 27squick srt in javaquicksort implementationquick sort best time complexityquicksort ccomplexidade assimptomatica quick sortquicksort wikipediaquickenquicksort in place quicksortpuort element in quick sortexplain quick sort algorithmwhat is the average case complexity of quick sortpartition sort length quicksort complexityquick sort time complexityspace complexity for quicksortwrite short note on 3a a 29 discrete optimization problems b 29 parallel quick sortquick sort complexity best casequicksort passwise outputquicksort algorithm by lengthquicksort workingthe quicksort algorithm can be used toexample array for quick sort to testquick sort time complexir 3dty if sortedquicksort inoperating systemsquick sort algorithm cpartition algorithm for quicksortquicksort explainedthe average time complexity of quicksort is 3fquick sorting algorithmfunction for quick sort in cquickest sort algorithmaverage case time complexity of the quick sort algorithm is more than quicksort time complexity is based on pivotquicksort 28int 5b 5d 29quick sort worst case big o notationquick sort program in c using partitionquick sort c functionquicksort algorithm c 2b 2bquicksort analysisquicksort 28 29 algorithmaverage complexity of quicksortquick sort algorithm codewrite a c program to implement quick sort algorithmquicksort examplequicksort is aquick sorting cquicksort function in cquick sort using recursion in c19 29 write a program to implement quick sort using array as a data structure quick sort big oquicksort pwhat is the worst case and best case runtime of quick sort for sorting n number of dataquick sort explainquick sort c codequick sort algorithm in c step by stepquicksort in c bigoaverage case of quicksortis quick sort in placecode of quick sortpartition algorithmwrite an algorithm for quick sort and explain time complexity of quick sort with example quick sort best case time complexityquicksort code in cquick sort time complexity worst casequick sort in c examplequick sort using setquick sort time complexworst case time complexity of quick sortwhen to use quicksortquicksort pivot sortpivot element us taken in following sortingquicksort diagramquick sort conceptquick sort algorithm example in cquick sort c nao faz pra posicao do meiokquicksort 3aquicksandsort an array using quick sort in cpartition in quicksort time complexityquicksort space and time complexitypartition quicksort cquicksort time complexity best casesorting quicksortquick sort function in cquick sort algorithm examplesquick sort worst case time complexityquicksort highwhat is quick sort algorithmquick sort c programquick sort algorithm with pivotexplain quick sort code in cthe worst case time complexity of quick sort isthe time complexity of quicksortquicksort with last element as pivot examplequick sort algorutm cquicksort algorithm cquick sort in placehow does the quicksort algorithm workorder quicksortquick sort in c using tempwrite a c program to implement quick sort algorithm partition sort length nwhat is the worst case complexity of quicksort o 28n2 29 3fquicksort algorithm codewhat is the best case time complexity of quick sortimplement quick sort in cquicksort algorithmquicksort algorithm explainedquick sort arrayaverage case time complexity of quicksortquick sort in c cormenquick sort codemodify the following in place quicksort implementation of code to a randomized version of algorithmaverage case analysis of quicksortquicksort c 23 wikiquicksort average case space used quicksortquickest approximate sortquicksortr space complexity worst case time complexity of the quick sort algorithmtime complexity of quice sortquick sort algorithm cc quick sortquick sort with examplequick sort program in c codegreperquick sort execution timepartition in quicksort c 2b 2bquic sortquicksortsimple quick sort program in cexample of quick sortquick sort in c for sorting numbersquicksort explanationc quicksortjava quicksort 2 pointerquick sort time complexity best casequick sort array cimplementation of quick sort in cimplement quick sort using cquicksort algorithm quick sort examplesort quicksortquicksort big o classquicksort functionquick sort algorithmquick sort i cquicksort definationqucik sort complexityhow quicksort workstime complexity of quicksortworst case time complexity of quicksort in placeworst time complexity of quick sortwrite a program to quicksort in cquicksort complexityeasy quicksortquick sortquicksort hoaretime complexity for quick sortquicksort engquick sort an array in cquick sort using loopsquick sorysimple quicksortquick sort array in cquicksort javaquick sort program in c in one functionquick sort in ascending order in cquicksort 5c analysisthe average case complexity of quick sort for sorting n numbers isquick sort c program codequicksort c programworst case time complexity quick sortquicksort algorithm explanationwrite 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 program in ctime complexity quicksortbinary search and quick sort are both examples of what sort of algorithm 3fwrite a c program to sort a list of elements using the quicksort algorithmquicksort pass wise output coddewhat will be the array after 2 pass in quick sortquicksort bigoquick sort algorithm using compare in cquicksort tutorialquick sort searchquick sort wikiquick sort algorithm geeksforgeeksquick sort algorithm explainedquick osrt in placenew quicksort 28 29what is quick sortquick sort example program in chow is an integer array sorted in place using the quicksort algorithm 3ffounder of quicksortalgorithm time complexity quick sortquick sort best and worst case codeshort quick sort codetime complexity of quicksort in worst casequick sort space complexityquick sort inc cis quicksort algorithmquick sort program in c with code explanationaverage case time complexity of quick sortquick sort of array in cquick sort in cwhat is quick sort in cquicksort explained in cwhat is the average time complexity of quicksort 3fquick sort cquicksort sort algorithm in cquicksort partitioningplace and divide quick sort pivot in the middlec program sort the given number in ascending order quick sortquick sort big o average timesort function in c for arrayc quicksort codequicksort programaverage time quicksortaverage case complexity of quick sortpartition in quicksorty quicksort can optimal the schdulepartition algorithm complexitythe quick sort algorithm selects the first element in the list as the pivot revise it by selecting the median among the first 2c middle 2c and last elements in the list quick sort geesorted fast 3fwrite a c program to sort the given set of numbers by performing partition 28 29 function using the divide and conquer strategyquick sort averageprogram for quick sorting algorithmquicksort program in cquick sort sorted arraytime complexity of quicksort algorithmbest case running time for quicksortwhat is the best case efficiency for a quick sortwrite 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 recurrence equal for worst case of quick sort 3fhow the quicksort worksimplest definition of quicksortcost quicksortspace complexity of quicksortquick sort in cquickqortquick sort program in c with time complexityspace complexity quick sortbest and worst cases of quick sort with examplewhat is best case 2c worst case and average case complexity of quick sort 3fquick sort algorithm in cquick sort i arrayquick sort best casepass this temporary array to both the quicksort function and the partition functionquicksort space complexityquick sort analysisb quicksort half partitionwrite a program to sort the list using quick sort 28using function 29explain quicksortpivot element in quick sortin place quick sortquicksirt diagramsexplain quick sortquick sorting with pthread code in cin partition algorithm 2c the subarray has elements which are greater than pivot element x what is the time complexiry of quick sortproperties of quicksortc array quick sortquick sort implementation in cefficiency of quicksort algorithmplace and divide quicksort pivot in the middlequicksort conceptquicksort c codequick sort implementationwhen is quicksort usedexplain important properties of ideal sorting algorithm and complexity analysis of quick sort how does quicksort workeverything about quicksort in depthhow does quicksortquicksort with number of elementspseudocode for quick sort considering first element as pivot in cways to implement quicksortquick sort explanation in cque es quick sortquicksort in cquick sort c 2b 2b codequicksort in c code 23define quicksortwhat is quicksortspace complexity of quick sortquick sorting 5 10 15 1 2 7 8 quicksortquick sort in algorithmquicksort space complexityquick sort in c programspace complexity of quicik sortquicksort algorithm in cquick sort program in c 2b 2bsource code of quicksort with mid element as pivotwhen will quicksort workquicksort sort codewhy use the quick sort can optimal schedulequicksort algorithm timingtime complexity of quick sortquick sortc code for quick sortquicksort an array in choar quicksortquick sort easy program in cwho invented quicksortwhat is the worst case time complexity of a quick sort algorithm 3fquicksort mediaquick 5csort algorithmcode for partition funtion in cthe quick sort algorithmquick sort definitionquick sort using structure array in cquick sort to return a particular number in arrayhyk sort c languagearray quick sortsorting algorithm uses the value based partitionimplementation of quicksort in cquicksort algorithmusbest case time complexity of quicksortquicksort sortpartition sortquick sort complextyrequirements for quick sortquicksort average casevariation of quicksort 3aquick sort