quicksort

// @see https://www.youtube.com/watch?v=es2T6KY45cA&vl=en
// @see https://www.youtube.com/watch?v=aXXWXz5rF64
// @see https://www.cs.usfca.edu/~galles/visualization/ComparisonSort.html

function partition(list, start, end) {
    const pivot = list[end];
    let i = start;
    for (let j = start; j < end; j += 1) {
        if (list[j] <= pivot) {
            [list[j], list[i]] = [list[i], list[j]];
            i++;
        }
    }
    [list[i], list[end]] = [list[end], list[i]];
    return i;
}

function quicksort(list, start = 0, end = undefined) {
    if (end === undefined) {
        end = list.length - 1;
    }
    if (start < end) {
        const p = partition(list, start, end);
        quicksort(list, start, p - 1);
        quicksort(list, p + 1, end);
    }
    return list;
}

quicksort([5, 4, 2, 6, 10, 8, 7, 1, 0]);

0
8
Lionel Aguero 33605 points

                                    #include&lt;stdio.h&gt;
int partition(int arr[], int low, int high) {
  int temp;
  int pivot = arr[high];
  int i = (low - 1); 
  for (int j = low; j &lt;= high - 1; j++) {
    if (arr[j] &lt;= pivot) { 
      i++; 
      temp = arr[i];
      arr[i] = arr[j];
      arr[j] = temp;
    } 
  } 
  temp = arr[i + 1];
  arr[i + 1] = arr[high];
  arr[high] = temp;
  return (i + 1); 
} 
void quick_sort(int arr[], int low, int high) { 
  if (low &lt; high) {
    int pi = partition(arr, low, high); 
    quick_sort(arr, low, pi - 1); 
    quick_sort(arr, pi + 1, high); 
  } 
} 
int print(int arr[], int n) {
  for(int i = 0; i &lt; n; i++) {
    printf(&quot;%d &quot;, arr[i]);
  }
}

int main()
{
int n, i;
scanf(&quot;%d&quot;, &amp;n);
int arr[n];
for(i = 0; i &lt; n; i++)
{
scanf(&quot;%d&quot;, &amp;arr[i]);
}
quick_sort(arr, 0, n - 1);
print(arr, n);
}

0
0
4.43
7
Phoenix Logan 186120 points

                                    algorithm quicksort(A, lo, hi) is
    if lo &lt; hi then
        p&nbsp;:= partition(A, lo, hi)
        quicksort(A, lo, p - 1)
        quicksort(A, p + 1, hi)

algorithm partition(A, lo, hi) is
    pivot&nbsp;:= A[hi]
    i&nbsp;:= lo
    for j&nbsp;:= lo to hi do
        if A[j] &lt; pivot then
            swap A[i] with A[j]
            i&nbsp;:= i + 1
    swap A[i] with A[hi]
    return i

4.43 (7 Votes)
0
4.38
8
Awgiedawgie 440215 points

                                    /********** QuickSort(): sorts the vector 'list[]' **********/

/**** Compile QuickSort for strings ****/
#define QS_TYPE char*
#define QS_COMPARE(a,b) (strcmp((a),(b)))

/**** Compile QuickSort for integers ****/
//#define QS_TYPE int
//#define QS_COMPARE(a,b) ((a)-(b))

/**** Compile QuickSort for doubles, sort list in inverted order ****/
//#define QS_TYPE double
//#define QS_COMPARE(a,b) ((b)-(a))

void QuickSort(QS_TYPE list[], int beg, int end)
{
    QS_TYPE piv; QS_TYPE tmp;
    
    int  l,r,p;

    while (beg&lt;end)    // This while loop will substitude the second recursive call
    {
        l = beg; p = (beg+end)/2; r = end;

        piv = list[p];

        while (1)
        {
            while ((l&lt;=r) &amp;&amp; (QS_COMPARE(list[l],piv) &lt;= 0)) l++;
            while ((l&lt;=r) &amp;&amp; (QS_COMPARE(list[r],piv)  &gt; 0)) r--;

            if (l&gt;r) break;

            tmp=list[l]; list[l]=list[r]; list[r]=tmp;

            if (p==r) p=l;
            
            l++; r--;
        }

        list[p]=list[r]; list[r]=piv;
        r--;

        // Select the shorter side &amp; call recursion. Modify input param. for loop
        if ((r-beg)&lt;(end-l))   
        {
            QuickSort(list, beg, r);
            beg=l;
        }
        else
        {
            QuickSort(list, l, end);
            end=r;
        }
    }   
}

4.38 (8 Votes)
0
0
0
Phoenix Logan 186120 points

                                    //I Love Java
import java.io.*;
import java.util.*;
import java.util.stream.*;
import static java.util.Collections.*;

import static java.util.stream.Collectors.*;

public class Quick_Sort_P {

    static void swap(List&lt;Integer&gt; arr, int i, int j) {
        int temp = arr.get(i);
        arr.set(i, arr.get(j));
        arr.set(j, temp);
    }

    static int partition(List&lt;Integer&gt; arr, int low, int high) {

        int pivot = arr.get(high);
        int i = (low - 1);

        for (int j = low; j &lt;= high - 1; j++) {

            if (arr.get(j) &lt; pivot) {

                i++;
                swap(arr, i, j);
            }
        }
        swap(arr, i + 1, high);
        return (i + 1);
    }

    static void quickSort(List&lt;Integer&gt; arr, int low, int high) {
        if (low &lt; high) {

            int pi = partition(arr, low, high);

            quickSort(arr, low, pi - 1);
            quickSort(arr, pi + 1, high);
        }
    }

    public static void main(String[] args) throws IOException {

        BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));

        List&lt;Integer&gt; arr = Stream.of(buffer.readLine().replaceAll(&quot;\\s+$&quot;, &quot;&quot;).split(&quot; &quot;)).map(Integer::parseInt)
                .collect(toList());

        int n = arr.size();

        quickSort(arr, 0, n - 1);
        System.out.println(&quot;Sorted array: &quot;);
        System.out.println(arr);
    }
}

0
0
5
1
IllusiveBrian 18110 points

                                    //last element selected as pivot
#include &lt;iostream&gt;

using namespace std;
void swap(int*,int*);
int partition(int arr[],int start,int end)
{
    int pivot=arr[end];
    int index=start;
    int i=start;
    while(i&lt;end)
    {
        if(arr[i]&lt;pivot)
        {
            swap(&amp;arr[index],&amp;arr[i]);
            index++;
        }
        i++;
    }
    swap(&amp;arr[end],&amp;arr[index]);
    return index;
}
void quicksort(int arr[],int start,int end)
{
    if(start&lt;end)
    {
      int pindex=partition(arr,start,end);
      quicksort(arr,start,pindex-1);
      quicksort(arr,pindex+1,end);
    }
}
void display(int arr[],int n)
{
    for(int i=0;i&lt;n;i++)
    {
        cout&lt;&lt;arr[i]&lt;&lt;&quot; &quot;;
    }
    cout&lt;&lt;endl;
}

int main()
{
    int n;
    cout&lt;&lt;&quot;enter the size of the array:&quot;&lt;&lt;endl;
    cin&gt;&gt;n;
    int arr[n];
    cout&lt;&lt;&quot;enter the elements of the array:&quot;&lt;&lt;endl;
    for(int i=0;i&lt;n;i++)
    {
        cin&gt;&gt;arr[i];
    }
    cout&lt;&lt;&quot;sorted array is:&quot;&lt;&lt;endl;
    quicksort(arr,0,n-1);
    display(arr,n);

    return 0;
}
void swap(int *a,int*b)
{
    int temp=*a;
    *a=*b;
    *b=temp;
}

5 (1 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
is quick sort in place quicksort working quick sort in algorithm in place quick sort quick sort with example quick-sort code quicksort alo quick sort example the quick sort algorithm selects the first element in the list as the pivot. Revise it by selecting the median among the first, middle, and last elements in the list. quick sort concept explain quick sort algorithm the quick sort algorithm when is quicksort used quicksort hoare quick sort algorithm code new QuickSort() quick sort algorithm examples quick sort explain quickest sort algorithm quicksort algorithm code quick sort gee quick sorting algorithm quick \sort algorithm example of quick sort quicksort high quicksort program how does quicksort quick sort code quick sort analysis quicksort p what is quick sort algorithm quicksort implementation simple quicksort quicksort algorithmus simplest definition of quicksort quickest approximate sort quicksort sort code quicksort algorithm. when to use quicksort quicksort analysis quicksort \ analysis quicksort tutorial quick sort implementation quick sorting sorting quicksort how quicksort works quicksort java quick sort wiki quicksort example quicksort function quicksort concept how does quicksort work quick sort algorithm ' explain quicksort sort quicksort quicksort explanation who invented quicksort quicksort code quicksort defination hoar quicksort easy quicksort quicksort algorithm explanation partition sort length n partition sort length partition sort n Average case time complexity of the quick sort algorithm is more than how the quicksort work quicksort wiki requirements for quick sort quick sort use case what is best case worst case and average case for quick sort worst time complexity of quick sort average time quicksort quick sort time complexity best case how does the quicksort algorithm work quicksortr space complexity Quicksort is a quick sort average runtime quick sort in place quick sort time complexity worst case quicksort algorithm runtime quick sort BEst case best case complexity of quick sort how does a quicksort work partition algorithm quicksort big o class quicksort space complexity partition sort quicksort inoperating systems in place quicksort quicksort time complexity best case what is quicksort Explain important properties of ideal sorting algorithm and complexity analysis of quick sort. quicksort partitioning quicksand In partition algorithm, the subarray ______________ has elements which are greater than pivot element x. quicken quicksort in c bigo order quicksort quick sort execution time partition in quicksort time complexity worst case time complexity of quicksort in-place What is best case, worst case and average case complexity of quick sort? place and divide quicksort pivot in the middle place and divide quick sort pivot in the middle time complexity of quicksort in worst case best case running time for quicksort quicksort complexity quicksort explained quick sort space complexity quick sort c nao faz pra posicao do meio average complexity of quicksort quick sort time complex what is the worst case and best case runtime of quick sort for sorting N number of data The average case complexity of quick sort for sorting n numbers is time complexity of Quick sort in worst and best case using master theorem. quick sort average average case time complexity of Quicksort The quicksort algorithm can be used to quick sort algorithm best and worst cases of quick sort with example quicksort diagram average case of quicksort algorithm time complexity quick sort sorted fast? the time complexity of quicksort best case time complexity quick sort worst case time complexity quick sort time complexity of quice sort y quicksort can optimal the schdule why use the quick sort can optimal schedule What is the best case time complexity of Quick sort quicksort eng average case complexity of quick sort what is the time complexiry of quick sort time complexity for quick sort quicksort complexity worst case time complexity of the Quick sort algorithm what is the best case efficiency for a quick sort Binary Search and Quick Sort are both examples of what sort of algorithm? time complexity of quick sort what is the average case complexity of quick sort The average time complexity of Quicksort is? cost quicksort quick osrt in place quick sort big o average time quick sort big o partition algorithm complexity average case analysis of quicksort properties of quicksort what is the big o of quicksort que es quick sort quick sort time complexir=ty if sorted quicksort algorithm timing quicksort space and time complexity quicksort average case space used quicksort algorithm by length quicksort worst case time complexity quick sort worst case big o notation quick sort complexity best case space complexity of quicik sort pivot element in quick sort quicksort time complexity of quicksort algorithm best case time complexity of quicksort quicksort space complexity Quick sort worst case time complexity Write short note on : a) Discrete optimization problems b) Parallel quick sort why is quicksort conquer trivial What is the average time complexity of QuickSort? everything about quicksort in depth time complexity of quick sort in worst case quicksirt diagrams worst case time complexity of quick sort time complexity of Quicksort quick sort best time complexity What is the worst case time complexity of a quick sort algorithm? The worst-case time complexity of Quick Sort is quicksort time complexity is based on pivot b-quicksort half partition quicksort algorithm quicksort media space complexity for quicksort what is the worst case complexity of quicksort O(n2)? Recurrence equal for worst case of quick sort? java quicksort 2 pointer space complexity of quick sort quick sort best case time complexity space complexity of quicksort quick sort definition best time and worst time complexity for quick sort algorithm efficiency of quicksort algorithm quick sort best and worst case code quicksort C# wiki sorting algorithm uses the value based partition complexidade assimptomatica quick sort quicksort wikipedia quick sort complexty variation of QuickSort: KQuickSort: average case time complexity of quick sort quicksort time complexity time complexity graph of quicksort qucik sort complexity quick sort time complexity quicksort bigo quick sort quickqort space complexity quick sort quick-sort time complexity quicksort quicksort average case founder of quicksort quocksort quicksort
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source