parameters in function

closed account (zwpL3TCk)
Hi, I just wanna ask about the parameters in functions.
I'm currently studying the code i got from other site about quick sort algorithm.
There is some code i don't understand. Can someone explain me this codes?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void quicksort(int array[], int firstIndex, int lastIndex) // This is a function called quicksort.
                                   //The code inside the parameters is the declaration?
        pivotIndex = firstIndex; // This code and below codes can you explain how this codes work?
        index1 = firstIndex;
        index2 = lastInde


        while(index1 < index2) 
        {
            while(array[index1] <= array[pivotIndex] && index1 < lastIndex)
            {
                index1++;
            }
            while(array[index2]>array[pivotIndex])
            {
                index2--;
            }

Last edited on
What is it you don't understand?

Line 1: This serves are the function definition and also the declaration, if it hasn't previously been declared.

The snippet you posted is not complete. index1, index2 and pivotIndex are not defined.

closed account (zwpL3TCk)
I update the code i posted above.
Please post the entire program
Last edited on
closed account (zwpL3TCk)
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
#include<stdio.h>

void quicksort(int array[], int firstIndex, int lastIndex)
{

    int pivotIndex, temp, index1, index2;

    if(firstIndex < lastIndex)
    {

        pivotIndex = firstIndex;
        index1 = firstIndex;
        index2 = lastIndex;


        while(index1 < index2)
        {
            while(array[index1] <= array[pivotIndex] && index1 < lastIndex)
            {
                index1++;
            }
            while(array[index2]>array[pivotIndex])
            {
                index2--;
            }

            if(index1<index2)
            {

                temp = array[index1];
                array[index1] = array[index2];
                array[index2] = temp;
            }
        }


        temp = array[pivotIndex];
        array[pivotIndex] = array[index2];
        array[index2] = temp;

        quicksort(array, firstIndex, index2-1);
        quicksort(array, index2+1, lastIndex);
    }
}

int main()
{
    //Declaring variables
    int array[100],n,i;

    printf("Enter the number of element you want to Sort : ");
    scanf("%d",&n);

    printf("Enter Elements in the list : ");
    for(i = 0; i < n; i++)
    {
        scanf("%d",&array[i]);
    }

    quicksort(array,0,n-1);

    printf("Sorted elements: ");
    for(i=0;i<n;i++)
    printf(" %d",array[i]);

    return 0;
}

From line 3 to line 42 and line 60.
Last edited on
Topic archived. No new replies allowed.