error with quick sort

Hello Everyone, I have created a Quick Sort Algorithm, but I get error Segmentation Fault, whenever I run in Linux Terminal.

After displaying the array, I get this error.
Here is the code for it

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

#include<iostream>

using namespace std;
int n;
int b[20];

void qsort(int left,int right)
{
    int i,p,j;
    int temp =0;
    if (right > left)
    {
        i = left;
        j= right;
        p = b[left];
        
        while ( right > left)
        {
            do
            {
                i++;
                }
            while ((b[i] <=p) && (i<=right));
            while ( b[j] >= p && j>left)
            {
                --j;
                }
            if (j>i)
            {
                temp = b[i];
                b[i] = b[j];
                b[j] = temp;
                }
            }
        temp = b[left];
        b[left] = b[j];
        b[j] = temp;
        qsort(left,j-1);
        qsort(i,right);
        }
    }

int main()
{
    int i,l,r;
    cout << "enter no. of elemets:"<<endl;
    cin >> n;
    cout << "\n enter elements:"<< endl;
    for ( i=0;i<n;i++)
    {
        cin >> b[i];
        }
    cout << "\n elements of array : \n"<< endl;
    for (i=0;i<n;i++)
    {
        cout << b[i]<<endl;
        }
    l =0;
    r = n-1;
    qsort (l,r);
    cout << "\n after quick sort:\n"<<endl;
    for (i =0;i<n;i++)
    {
        cout << b[i]<<"\t";
        }
    return 0;
    } 
    
    


Help please??
What value are you using for "n"?
it will be entered by user.
Program will ask for
Enter no. of elements:
then n will be entered.
I don't see where you are protecting the array named "b" from someone typing in a value > 20. That would do it!
Replace your array by a vector<int> and recompile with -D_GLIBCXX_DEBUG to catch out-of-bound accesses.
Additionally, step through your program line by line and check where things deviate from what you expect and why.

Edit: here we go:
1
2
3
4
5
6
7
/usr/include/c++/4.6/debug/vector:313:error: attempt to subscript container 
    with out-of-bounds index 20, but container only holds 20 elements.

Objects involved in the operation:
sequence "this" @ 0x0x608460 {
  type = NSt7__debug6vectorIiSaIiEEE;
}

and the debugger points to line 24 of your code.
Good luck.
Last edited on
Well dude, I'm gonna be completely honest here and say that I attempted to write my own version of the code that would sort the array and it really (excuse my language) f***** my head over.

So good luck with that.

However what I can offer you is a more user friendly way of entering the array elements, what it does is take in the elements of the array and prints them out. After that (the sorting), I'm not sure. Good luck to you.

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
#include<iostream>
using namespace std;

int main() {
	
	int certainarray[20] = {0};
	int noelements = 0;
	int element = 0;
	int noarray = 0;

	cout << "Enter no. of elements you would like to insert into the array (max is 20)." << endl;
	cin >> noelements;
	cout << "Please enter the elements of your array in consecutive order (integers only)." << endl;

	while (true) {
		if (cin >> element) {
			certainarray[noarray] = element;
			noarray++;}
		if (noarray == noelements) {
			break;}}

	cout << "The current elements of your array are: ";

	for (int i = 0; i < noelements; i++){
		cout << certainarray[i] << " ";}


	while (true) {}

	return 0;}



Ah, I think I found a problem, the function is call-by-value, so it won't actually change the elements of the array I believe. I think you should start from scratch and maybe use a bunch of nested while or for loops instead to change the array.

I suggest creating a new array based on the old array. And then printing a new array.

Cuz when I pasted in your qsort it said b[/*whatever is in here*/] in the function definition was an undeclared identifier or something (I think), even though I changed my array up top to b[20], so it may be that the function was looking for a local array called b[/*whatever is in here*/], instead of the global b.

I don't know, I may be wrong.
Last edited on
While we're at it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
  using namespace std;
  cout << "Please enter the numbers you'd like to sort (EOF to abort):" << endl;
  vector<int> numbers;
  int number;
  while (cin >> number)numbers.push_back(number);
  sort(numbers.begin(),numbers.end());
  cout << endl << "Your sorted numbers:" << endl;
  for (int v : numbers)cout << v << ' ';
  cout << endl;
}
Topic archived. No new replies allowed.