Using Quicksort With Struct Array

Run-Time Check Failure #3 - The variable 'pivot' is being used without being initialized. how to fix?
can someone help me ?

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
  void quick_sort(values dizi[], int first,int last) {
    int i, j, pivot,temp;
    float tempdolar;
    string temptarih;
    
    if (first < last) {
        pivot = first;
        i = first;
        j = last;
        while (i < j) {
            while (dizi[i].deger <= dizi[pivot].deger &&i< last) {
                i++;
            }
            while (dizi[j].deger > dizi[pivot].deger) {
                j--;
            }
            if (i < j) {
                tempdolar = dizi[i].deger;
                dizi[i].deger = dizi[j].deger;
                dizi[j].deger = tempdolar;
                temptarih = dizi[i].tarih;
                dizi[i].tarih = dizi[j].tarih;
                dizi[j].tarih = temptarih;
            }
        }
    }
    tempdolar = dizi[pivot].deger;  // here problem, this pivot.
    dizi[pivot].deger = dizi[j].deger;
    dizi[j].deger = tempdolar;
    quick_sort(dizi, first, j - 1);
    quick_sort(dizi, j + 1, last);
    
}
If first >= last, your pivot variable is never set, because your if statement is never entered.
what should I do?
current code. I don't know what the problem is.

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
  void quick_sort(values dizi[], int first,int last) {
    int i = 0, j = 0, pivot = 0; float temp = 0;
    float tempdolar=0;
   
    
    
    if (first < last) {
        pivot = first;
        i = first;
        j = last;
        while (i < j) {
            while (dizi[i].deger <= dizi[pivot].deger &&i< last) {
                i++;
            }
            while (dizi[j].deger > dizi[pivot].deger) {
                j--;
            }
            if (i < j) {
                tempdolar = dizi[i].deger;
                dizi[i].deger = dizi[j].deger;
                dizi[j].deger = tempdolar;
            }
        }
    }

    tempdolar = dizi[pivot].deger;
    dizi[pivot].deger = dizi[j].deger;
    dizi[j].deger = tempdolar;
    quick_sort(dizi, first, j - 1);
    quick_sort(dizi, j + 1, last);
    
}
Last edited on
It's up to you to ask smart questions.

You don't know what's wrong, but do you know what's right?
Certainly, there is something that your program should do.

- What should your program do?
- What is the difference between what your program should do, and what it's doing now?
- Explain this difference as best you can. Is it crashing? Is it producing wrong values? Is it an infinite loop? etc.

While you're thinking about that, think about what the purpose of the pivot is in the ideal quicksort algorithm, and what the possible values of pivot are in your program.
https://www.hackerearth.com/practice/algorithms/sorting/quick-sort/tutorial/
Last edited on
thanks for your helpful resource.
Use the debugger to trace through the code, watching the contents of variables etc and when what happens deviates from that expected then you're found an issue.
Topic archived. No new replies allowed.