Help with Search & sorting

Hello, I am needing some help with searching and sorting. We have to create a program that asks the user if they want to run a bubble sort or a selection sort & then it asks them if they want to do a linear or binary search or both. These are the exact instructions.. Any help would be greatly appreciated.

Requirements:
1. We are dealing with sorting and searching. You should include this array deļ¬nition to do sorting and searching operations.
int unsorted [] = { 85, 64, 16, 18, 1, 88, 48, 63, 54, 83, 79, 50, 0, 55, 17, 99, 69, 53, 65, 22, 93, 86, 9, 37, 56, 23, 21, 52, 78, 29, 14, 58, 35, 47, 68, 87, 3, 34, 5, 74, 4, 45, 41, 49, 67, 89, 92, 60, 72, 20, 8, 15, 42, 71, 31, 36, 90, 84, 70, 51, 28, 32, 81, 10, 82, 40, 57, 24, 25, 91, 44, 66, 30, 62, 94, 6, 7, 46, 43, 38, 75, 11, 39, 80, 98, 27, 12, 76, 96, 2, 77, 19, 26, 59, 33, 73, 13, 61, 95, 97 };

2. You should allow the user to choose between sorting algorithms.

3. You should also provide another option to test and compare the searching algorithms.

4. You should be implementing all searching and sorting algorithms we discussed in class and you should be displaying how many steps it took for algorithm to search or sort the array.

Bonus: Include insertion sort


The output should look like: (please note that this is merely an example and numbers are not correct!) -------------------------------------------------------------------------------------------------------------------------------------------

Which algorithm should be used to sort? (0:Bubble Sort, 1:Selection Sort)
1
45 swaps performed with Selection Sort...

Which algorithm should be used to search? (0:Linear Search, 1:Binary Search, 2:Test Both)
2

What do you want to search?
45
Linear Search retrieved the item in 46 steps...
Binary Search retrieved the item in 5 steps...
------------------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------------------
Which algorithm should be used to sort? (0:Bubble Sort, 1:Selection Sort)
0
28 swaps performed with Bubble Sort...

Which algorithm should be used to search? (0:Linear Search, 1:Binary Search, 2:Test Both)
1
What do you want to search?
79
Binary Search retrieved the item in 7 steps...
-------------------------------------------------------------------------------------------------------------------------------------------



This is my code so far.. I'm not sure if i'm doing it right...

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168

#include <iostream>
#include <cstdlib>
#include <string>

using namespace std;


const int SIZE = 100;
int unsorted [SIZE] = { 85, 64, 16, 18, 1, 88, 48, 63, 
					54, 83, 79, 50, 0, 55, 17, 99,  
					69, 53, 65, 22, 93, 86, 9, 37, 
					56, 23, 21, 52, 78, 29, 14, 58, 
					35, 47, 68, 87, 3,  34, 5, 74, 
					4, 45,41, 49, 67, 89, 92, 60, 
					72, 20, 8, 15, 42, 71, 31, 36, 
					90, 84,  70, 51, 28, 32, 81, 10, 
					82, 40, 57, 24, 25, 91, 44, 66,
					30, 62, 94, 6, 7, 46, 43,  38, 
					75, 11, 39, 80, 98, 27, 12, 76, 
					96, 2, 77, 19, 26, 59, 33, 73, 
					13, 61, 95, 97 }; 
					
void sortArray(int [], int);
void selectionSort (int [], int);


int main()
{
	int userChoiceSort;
	int userChoiceSearch;
	int findValue;
	int numElems;
	int searchList ();
	int binarySearch();
	
	
	cout << "Hi there! " << endl;
	cout << "What type of sort would you like to use? " << endl;
	cout << "**Bubble Sort = 1** OR **Selection sort = 2**" << endl;
	cin >> userChoiceSort;
	
	if (userChoiceSort = 1)
	{
		sortArray(unsorted, SIZE);
	}
	
	else if (userChoiceSort = 2)
	{
		selectionSort(unsorted, SIZE);
	}
	else 
	{
		cout << "Incorrect Input!!!" << endl;
	}
	
	cout << "How would you like to search?" << endl;
	cout << "**Linear Search = A** OR **Binary Search = B** OR **Both = C**" << endl;
	cin >> userChoiceSearch;
	cout << "What would you like to search for? **You may choose between 0-100**" << endl;
	cin >> findValue;
	
	if (userChoiceSearch == 'a' || userChoiceSearch == 'A' )
	{
		searchList();
	}
	//else 
	//{
		
	//}
	





	return 0;
}

//Runs bubble sort 
void sortArray(int unsorted[], int SIZE )
{
	bool swap;
	int temp;
	
	do 
	{
		swap = false;
		for (int count = 0; count < (SIZE - 1); count ++)
		{
			if (unsorted [count] > (unsorted [count + 1] ))
			{
				temp = unsorted[count];
				unsorted[count] = unsorted[count + 1];
				unsorted[count + 1] = temp;
				swap = true;
			}
		}
	} while (swap);
}

//Runs selection sort 
void selectionSort (int unsorted[], int SIZE )
{
	int startScan;
	int minIndex;
	int minValue;
	
	for (startScan = 0; startScan < (SIZE - 1); startScan++)
	{
		minIndex = startScan;
		minValue = unsorted[startScan];
		for (int index = startScan + 1; index < SIZE; index ++)
			if (unsorted [index] < minValue)
			{
				minValue = unsorted[index];
				minIndex = index;
			}
		unsorted[minIndex] = unsorted[startScan];
		unsorted[startScan] = minValue;
	}
}

//If linear 
int searchList(int unsorted[], int numElems, int findValue)
{
	int index = 0; //Subscript to search array
	int position = -1; //Record position of value
	bool found = false; //Indicated value is found

	while (index < numElems && !found)
	{
		if (unsorted[index] == findValue) //If found
		{
			found = true; //set the flag
			position = index; //Record the values subscript
		}
		index++; //Go to next element 
	}
	return position;
}

int binarySearch(int unsorted[], int SIZE, int findValue)
{
	int first = 0; //First array element
	int last = SIZE -1; //Last array element
	int middle; //Mid point of search
	int position = -1; //Position of search value
	
	bool found = false; //Flag
	
	while (!found && first <= last)
	{
		middle = (first + last) / 2; //Calculate mid point
		if (unsorted[middle] == findValue) //If value is at mid
		{
			found = true;
			position = middle;
			
		}
		else if (unsorted [middle] > findValue) //If value is in lower half
			last = middle - 1;
		else 
			first = middle + 1; //If value is in upper half
		
	}
	return position;
}
Last edited on
so uhh.. did you finish this code? I also ran your code and you have to set your userChoice statements in the the if else to == not = to fix the error. other than that once you complete the search list and use the numelem you should be good.
Id like to see the final product
Topic archived. No new replies allowed.