problem with quicksort function

Pages: 12
i need to sort a list of 10 numbers using the QuickSort method but there are errors in the split function. All the < and > are underlined and say no operator "<" matches these operands

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
#include "stdafx.h"
using namespace std;

typedef struct 
 {
	int first;
	int last;
	int splitPt1;
	int splitPt2;
 }values ;

typedef values ItemType; 



void Split(ItemType values[],int first, int last,int& splitPt1, int& splitPt2);
void QuickSort (ItemType values[], int first, int last);

int _tmain(int argc, _TCHAR* argv[])
{
	int list[10];
	
	//code: get data to fill the collection
	cout << "Enter 10 positive integers for the list\n";
	for (int i = 0; i < 10; i++)
	cin >> list[i];
	

	return 0;
}

void Split(ItemType values[],int first, int last,int& splitPt1, int& splitPt2)
{
	ItemType splitVal = values[(first+last)/2],t;
   int i = first, j = last;
   while (values[i] < splitVal) i++;
	while (values[j] > splitVal) j--;
   while (i < j)
   {
      t = values[i];      values[i] = values[j]; 
      values[j] = t;
		i++;	
		j--;
	while (values[i] < splitVal) i++;
	while (values[j] > splitVal) j--;
   }
   if (i == j)
   {
      i++; j--;
   }
   splitPt1 = i;
   splitPt2 = j;
}

void QuickSort (ItemType values[], int first, int last)
{	
	int splitPt1, splitPt2;
	if (first < last)
	{
		Split(values, first, last, splitPt1, splitPt2);
		QuickSort(values, first, splitPt2);
		QuickSort(values, splitPt1, last);
	
	}
}
anyone? please help
You're comparing structs with each other. What's the criteria that one struct is less/greater then the other?

If you want to do it in C you need functions such as greater(const ItemType &i1, const ItemType &i2) to tell what item is greater.
i don't really understand could you show me?
Where is your #include<iostream> ?
in stdafx.h
What is this
1
2
3
4
5
6
7
8
9
typedef struct 
 {
	int first;
	int last;
	int splitPt1;
	int splitPt2;
 }values ;

typedef values ItemType;
good for? I don't see any use.
I wasnt sure how to initialize all the values needed for the two functions
But you don't use it at all.

Drop that struct and replace ItemType with int and the compiler errors will go away
i changed the code a bit
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
int list[10];
void Split(list[],int first, int last,int& splitPt1, int& splitPt2);
void QuickSort (ItemType values[], int first, int last);

int _tmain(int argc, _TCHAR* argv[])
{

	//code: get data to fill the collection
	cout << "Enter 10 positive integers for the list\n";
	for (int i = 0; i < 10; i++)
	cin >> list[i];
	

	return 0;
}

void Split(list[],int first, int last,int& splitPt1, int& splitPt2)
{
	list splitVal = values[(first+last)/2],t;
   int i = first, j = last;
   while (values[i] < splitVal) i++;
	while (values[j] > splitVal) j--;
   while (i < j)
   {
      t = values[i];      values[i] = values[j]; 
      values[j] = t;
		i++;	
		j--;
	while (values[i] < splitVal) i++;
	while (values[j] > splitVal) j--;
   }
   if (i == j)
   {
      i++; j--;
   }
   splitPt1 = i;
   splitPt2 = j;
}

void QuickSort (list[], int first, int last)
{	
	int splitPt1, splitPt2;
	if (first < last)
	{
		Split(values, first, last, splitPt1, splitPt2);
		QuickSort(values, first, splitPt2);
		QuickSort(values, splitPt1, last);
	
	}

basically i just changed ItemType to list but im still getting errors
Last edited on
can someone please help?
If you want to keep 'ItemType'. It's okay

Then
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
#include "stdafx.h"
using namespace std;

typedef int ItemType; // ItemType is now int and it should compile



void Split(ItemType values[],int first, int last,int& splitPt1, int& splitPt2);
void QuickSort (ItemType values[], int first, int last);

int _tmain(int argc, _TCHAR* argv[])
{
	int list[10];
	
	//code: get data to fill the collection
	cout << "Enter 10 positive integers for the list\n";
	for (int i = 0; i < 10; i++)
	cin >> list[i];
	

	return 0;
}

void Split(ItemType values[],int first, int last,int& splitPt1, int& splitPt2)
{
	ItemType splitVal = values[(first+last)/2],t;
   int i = first, j = last;
   while (values[i] < splitVal) i++;
	while (values[j] > splitVal) j--;
   while (i < j)
   {
      t = values[i];      values[i] = values[j]; 
      values[j] = t;
		i++;	
		j--;
	while (values[i] < splitVal) i++;
	while (values[j] > splitVal) j--;
   }
   if (i == j)
   {
      i++; j--;
   }
   splitPt1 = i;
   splitPt2 = j;
}

void QuickSort (ItemType values[], int first, int last)
{	
	int splitPt1, splitPt2;
	if (first < last)
	{
		Split(values, first, last, splitPt1, splitPt2);
		QuickSort(values, first, splitPt2);
		QuickSort(values, splitPt1, last);
	
	}

} 
ok thanks it compiles but how do i display the list after its been sorted? i know i will need to call the QuickSort function
hello?
to display the list you just need to write loop like on line 17/18 and exchange 'cin >>' with 'cout <<'
but i need the list to get sorted so how do i call the quicksort function to sort the list?

QuickSort(list, 0, 10); after line 18
ok so heres what i added
1
2
3
4
QuickSort(list, 0, 10); 
cout << "sorted list is " << endl;
for (int list = 0; list < 10; list++)
cout << list;


but an error message comes up when i run it saying stack around variable 'list' was corrupted.
I tried the for loop using i instead of list but it still doesnt work
hm, ok it's 9 not 10: QuickSort(list, 0, 9);
ok that got rid of the error message but the output isnt right

Enter 10 positive integers
22 54 12 1 8 7 4 2 98 5
sorted list is
0043FB7C 0043FB7C 0043FB7C 0043FB7C 0043FB7C 0043FB7C 0043FB7C 0043FB7C 0043FB7C 0043FB7C 
Press any key to continue . . .

Pages: 12