Answers for "quicksort example"

C
3

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]);
Posted by: Guest on May-31-2020
3

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]);
Posted by: Guest on May-31-2020
0

quicksort

//last element selected as pivot
#include <iostream>

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<end)
    {
        if(arr[i]<pivot)
        {
            swap(&arr[index],&arr[i]);
            index++;
        }
        i++;
    }
    swap(&arr[end],&arr[index]);
    return index;
}
void quicksort(int arr[],int start,int end)
{
    if(start<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<n;i++)
    {
        cout<<arr[i]<<" ";
    }
    cout<<endl;
}

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

    return 0;
}
void swap(int *a,int*b)
{
    int temp=*a;
    *a=*b;
    *b=temp;
}
Posted by: Guest on May-26-2021
0

quicksort

//last element selected as pivot
#include <iostream>

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<end)
    {
        if(arr[i]<pivot)
        {
            swap(&arr[index],&arr[i]);
            index++;
        }
        i++;
    }
    swap(&arr[end],&arr[index]);
    return index;
}
void quicksort(int arr[],int start,int end)
{
    if(start<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<n;i++)
    {
        cout<<arr[i]<<" ";
    }
    cout<<endl;
}

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

    return 0;
}
void swap(int *a,int*b)
{
    int temp=*a;
    *a=*b;
    *b=temp;
}
Posted by: Guest on May-26-2021

Code answers related to "C"

Browse Popular Code Answers by Language