i need help!!!! please help me

i need help to explain the step of this following program. it a quick sort. PLEASE HELP ME IF NOT IM DEAD TOMORROW

#include<stdio.h>

void quicksort(int [10],int,int);

int main(){
int x[20],size,i;

printf("Enter size of the array: "); //
scanf("%d",&size);

printf("Enter %d elements: ",size); //
for(i=0;i<size;i++)
scanf("%d",&x[i]);

quicksort(x,0,size-1);

printf("Sorted elements: "); //
for(i=0;i<size;i++)
printf(" %d",x[i]);

return 0;
}

void quicksort(int x[10],int first,int last){
int pivot,j,temp,i;

if(first<last){
pivot=first;
i=first;
j=last;

while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}

temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);

}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include<stdio.h>

void quicksort(int [10], int, int);

int main()
{
    int x[20];
    int Size;
    int PlaceInArray;

    printf("Enter Size of the array: "); //
    scanf("%d", &Size);

    printf("Enter %d elements: ",Size); //

    for(PlaceInArray = 0; PlaceInArray < Size; PlaceInArray++)
    {
        scanf("%d", &x[PlaceInArray]);
    }

    quicksort(x, 0, (Size - 1));

    printf("Sorted elements: "); //

    for(PlaceInArray = 0; PlaceInArray < Size; PlaceInArray++)
    {
        printf(" %d", x[PlaceInArray]);
    }

    return 0;
}

void quicksort(int x[10], int first, int last)
{
    int Pivot;
    int HighElementInArray;
    int temp;
    int LowElementInArray;

    if(first < last)
    {
        Pivot               = first;
        LowElementInArray   = first;
        HighElementInArray  = last;

        while(LowElementInArray < HighElementInArray)
        {
            while(x[LowElementInArray] <= x[Pivot] && LowElementInArray < last)
            {
                LowElementInArray++;
            }

            while(x[HighElementInArray] > x[Pivot])
            {
                HighElementInArray--;
            }

            if(LowElementInArray < HighElementInArray)
            {
                temp                    = x[LowElementInArray];
                x[LowElementInArray]    = x[HighElementInArray];
                x[HighElementInArray]   = temp;
            }
        }

        temp                   = x[Pivot];
        x[Pivot]               = x[HighElementInArray];
        x[HighElementInArray]  = temp;

        quicksort(x, first, (HighElementInArray - 1));

        quicksort(x, (HighElementInArray + 1), last);

    }
}


Do you see what some white space formatting and descriptive variable names can do to the readability of your code? Changing the variable names wasn't difficult either, every IDE and text editor out there supports find and replace. Nothing I did should have changed the functionality of your code.

The second while loop, starting at Line 48 in my revision, searches the array for an element that has a value lower then the pivot point. The third while loop searches for an element in the array with a value higher then the pivot point. The if statement at Line 58 in my revision switches them. You can explain the rest.

Topic archived. No new replies allowed.