quicksort/qs function runtime error

I am having a problem with a typo in my quicksort/qs project. 'enduser000' asked about these functions citing the same book (re: Forum/Beginners/2113).

My code:

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
//	Project 5-1

/*	QSDemo.cpp
	Program to demonstrate the QuickSort function
*/

#include <iostream>
#include <cstring>
using namespace std;

void quicksort(char *items, int len);

void qs(char *items, int left, int right);

int main () {
	
	char *StrArray = "abcdefghijk";

	int Length = strlen(StrArray);

	quicksort(StrArray, Length);

	return 0;
}
void quicksort(char *items, int len)
{
	qs(items, 0, len-1);
}
void qs(char *items, int left, int right)
{
	int i, j;
	char x;

	i = left; j = right;
	x = items[((left + right)/2-2)];

	do {
	  while((items[i] < x) && (i < right)) i++;
	  while((x < items[j]) && (j > left)) j--;

	    if(i<=j){
		   char y=items[i];
   		   items[i]=items[j]; // [it breaks here (EDIT: pulled 'cout's *& step-debuged)]
 		   items[j]=y;
		   i++;
		   j--;
		}

	} while(i<=j);

	if(left < j) qs(items, left, j);
	if(i < right) qs(items, i, right);

}


I don't get any compile warnings or errors, just a runtime error.

Last thing I can think of: MS VC++ Express Edition 2008.

Thanks in advance,

ERandall

P.S.: Thanks to Greywolf and thread http://www.cplusplus.com/forum/articles/1624/ for how to post actual code-rn [last edit]
Last edited on
At line 43 you are trying to write in a reserved part of memory, change line 17 to
char StrArray[] = "abcdefghijk"; so you will have access to the memory in which your string is stored
Last edited on
Thank you, Bazzy

I knew another person's proofread would do the trick.

BTW: It was my typo, not H.S.'s, his code was right. I just kept looking at it w/o seeing it.

Let that be a lesson: Don't get wrapped up checking where the program fails--LOOK EVERYWHERE!!.

Thanks again

ERandall
Topic archived. No new replies allowed.