Sort me this way

QUESTION

Given an array A consisting of integers of size N, you need to sort this array in non-decreasing order on the basis of the absolute value of the integers in the array. Print the sorted array to output then. Hint: Use quicksort to sort the given numbers\n\nInput:\n\nThe first line consists of a single integer N, the number of elements in the array. The next line consists of N space separated elements. No two elements in the array will have same absolute value.\n\nOutput:\n\nYou need to print the absolute sorted array. See the sample output for clarification.\n\nConstraints:\n\n1<=N<=10^5\n10^9<=A[i]<=10^9 \nA[i] is the i th element of the array.

“TESTCASE_1”: “10\n9 -10 -11 20 1 2 -3 4 -5 6\n###—###SEPERATOR—###—\n1 2 -3 4 -5 6 9 -10 -11 20”, “TESTCASE_2”: “10\n\n-905372717 954279408 531497782 552755670 144897683 539731202 -158669298 -691900066 889008512 215450287\n###—###SEPERATOR—###—\n144897683 -158669298 215450287 531497782 539731202 552755670 -691900066 889008512 -905372717 954279408”, “TESTCASE_3”: “100\n-960478381 -714850315 -772513777 -800616588 884325357 886610795 2155501 -232037830 -482141161 -266148849 -268212238 -663164637 838950321 691625069 -919182610 66634320 368049805 508619338 -534010975 -857928876 430071517 -489990278 -165483696 233741473 -815288875 820545540 355697922 63281873 -396945576 -156032897 143084260 753338230 709184553 -504213997 423709844 514048209 811715246 -138524728 726066592 -329856500 -724672873 477372930 486628567 127535387 -594447 904735030 -211268202 507329121 772547178 16164633 -78718693 755071085 -386767679 720149230 -118378364 359222440 52924120 775938381 -82282875 -128159531 892074753 79072378 -396000522 728828412 302977320 69837425 -127722236 272129950 -571020673 -479840536 -894801260 -532450272 -528174696 -339794416 916895123 545851243 -353364857 617345533 63841471 770429429 -943683339 -601031153 517691714 290928192 -717956633 630627774 462348054 -412999622 -176499402 306013023 73264334 754880751 329304238 175994567 423432931 464997201 -639207200 -143554548 -791705807 -950644003\n###—###SEPERATOR—###—\n-594447 2155501 16164633 52924120 63281873 63841471 66634320 69837425 73264334 -78718693 79072378 -82282875 -118378364 127535387 -127722236 -128159531 -138524728 143084260 -143554548 -156032897 -165483696 175994567 -176499402 -211268202 -232037830 233741473 -266148849 -268212238 272129950 290928192 302977320 306013023 329304238 -329856500 -339794416 -353364857 355697922 359222440 368049805 -386767679 -396000522 -396945576 -412999622 423432931 423709844 430071517 462348054 464997201 477372930 -479840536 -482141161 486628567 -489990278 -504213997 507329121 508619338 514048209 517691714 -528174696 -532450272 -534010975 545851243 -571020673 -601031153 617345533 630627774 -639207200 -663164637 691625069 709184553 -714850315 -717956633 720149230 -724672873 726066592 728828412 753338230 754880751 755071085 770429429 -772513777 772547178 775938381 -791705807 -800616588 811715246 -815288875 820545540 838950321 -857928876 884325357 886610795 892074753 -894801260 904735030 916895123 -919182610 -943683339 -950644003 -960478381”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include <stdio.h>
 
int Partition(int A[],int p,int r){
    int x,i,temp=0,j;
    x = abs(A[r]);
    i = p-1;
    for(j =p;j<r;j++){
        if(abs(A[j])<= x){
        i = i+1;
        temp = A[i];
        A[i] = A[j];
        A[j] = temp;
        }
    }
    int temp1=0;
    temp1 = A[i+1];
    A[i+1] = A[r];
    A[r] = temp1 ;
    return i+1 ;
}
void QuickSort(int A[],int p,int r){
    int q=0;
    if(p<r)
    {
        q = Partition(A,p,r);
        QuickSort(A,p,q-1);
        QuickSort(A,q+1,r);
    }
}
int main(){
    int n,i ;
    scanf("%d",&n);
    int a[n];
    for(i =0;i<n;i++)
        scanf("%d",&a[i]);
    QuickSort(a,0,n-1);
    for(i =0;i<n;i++)
        printf("%d ",a[i]);
 return 0;   
}

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
CHP Adblock Detector Plugin | Codehelppro