Quicksort 2 - Sorting

Sort by

recency

|

200 Discussions

|

  • + 0 comments

    This solution is going crazy for me: I have tried like this

    I have this code:

    function processData(input) {
        //Enter your code here
        //left<p, right>p
        let ar;
        let p = input[0]
        let left = []
        let right = []
    
    
        if (input.length <= 1) return input
        else {
    
            for (let i = 1; i < input.length; i++) {
                if (input[i] > p) {
                    right.push(input[i])
                } else {
                    left.push(input[i])
                }
            }
      
            ar = [...processData(left.slice(0).filter((d) => d < p)), p, ...processData(right.slice(0).filter((d) => d > p))]
            
        }
        console.log(...ar)
    
        return ar
    
    }
    
    
    
    process.stdin.resume();
    process.stdin.setEncoding("ascii");
    _input = "";
    process.stdin.on("data", function (input) {
        _input += input;
    });
    
    
    process.stdin.on("end", function () {
        processData(_input.replace(/\s+$/g, '').split('').map((num) => parseInt(num), 10));
    });
    

    And output is like this:

    2 3
    1 2 3
    1 2 3 5 //How to get rid of this part
    8 9 // and this part
    1 2 3 5 7 8 9
    

    if anybody knows please suggests me!!

    `

  • + 0 comments

    Python

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    
    isize = input()
    inp = input()
    inp = inp.split(" ")
    arr = []
    for i in inp:
        arr.append(int(i))
    
    def quicksort(arr):
        if (len(arr)<=1):
            return arr
        fv = arr[0]
        left = []
        right = []
        equal = [fv]
        for i in range(1, len(arr)):
            if (arr[i]< fv):
                left.append(arr[i])
            elif (arr[i] > fv):
                right.append(arr[i])
            else:
                equal.append(arr[i])
        left = quicksort(left)
    
        right = quicksort(right)
        new_ord_arr = left+equal+right
        print(*new_ord_arr)
        return new_ord_arr
    
    quicksort(arr)
    
        
    
  • + 0 comments

    include

    include

    include

    include

    include

    void quickSort(int *ar, int size)

    {

    if(size > 1){

    int temp, pivot = ar[0], ub = size-1;

    for(int i = size-1; i >= 0; --i){

    if(ar[i] >= pivot){

    temp = ar[i];

    int j = i;

    while(j < ub){//Variation of Insertion Sorting

    ar[j] = ar[j+1];

    ++j;

    }

    ar[ub--] = temp;

    }

    }

    ++ub;

    quickSort(ar, ub);

    quickSort(ar+ub+1, size-ub-1);

    //Output

    for(int i = 0; i < size; ++i){

    printf("%d ", ar[i]);

    }

    printf("\n");

    }

    }

    int main(void)

    {

    int N;

    scanf("%d", &N);

    int ar[N];

    for(int i = 0; i < N; i++){

    scanf("%d", &ar[i]);

    }

    quickSort(ar, N);

    return 0; }

  • + 0 comments

    //heqder flies

    include

    include

    include

    include

    include

    //quicksort function

    void quickSort(int *a, int s) //s = size //p = pivot //temp is the temaprary variable { //conditional statement if(s > 1){

    int temp, p = a[0], up = (s-1);

    //loop condition for(int l = s-1; l >= 0; --l){

    if(a[l] >= p){

    temp = a[l];

    int m = l;

    while(m < up){

    a[m] = a[m+1];

    ++m;

    }

    a[up--] = temp;

    } }

    ++up;

    quickSort(a, up);

    quickSort(a+up+1, s-up-1);

    //statement for printing output

    for(int l = 0; l < s; ++l){

    printf("%d ", a[l]);

    }

    printf("\n"); }

    } //main block int main(void){ //n=no.of.elements

    int n;

    scanf("%d", &n);

    int a[n]; //scanning array

    for(int l = 0; l < n; l++) {

    scanf("%d", &a[l]);

    }

    //function call quickSort(a, n);

    //it reprasents '0' error return 0;

    }

  • + 0 comments

    include

    include

    include

    include

    include

    void quickSort(int *ar, int size)

    {

    if(size > 1){

    int temp, pivot = ar[0], ub = size-1;

    for(int i = size-1; i >= 0; --i){

    if(ar[i] >= pivot){

    temp = ar[i];

    int j = i;

    while(j < ub){//Variation of Insertion Sorting

    ar[j] = ar[j+1];

    ++j;

    }

    ar[ub--] = temp;

    }

    }

    ++ub;

    quickSort(ar, ub);

    quickSort(ar+ub+1, size-ub-1);

    //Output

    for(int i = 0; i < size; ++i){

    printf("%d ", ar[i]);

    }

    printf("\n");

    }

    }

    int main(void)

    {

    int n;

    scanf("%d", &n);

    int ar[n];

    for(int i = 0; i < n; i++){

    scanf("%d", &ar[i]);

    }

    quickSort(ar, n);

    return 0;

    }