Sort Array

Working on a coding problem and I'm stuck on a sort array. I need to write two functions (one to display the contents of an array, and another to sort the array). I have very little experience in sorting arrays, so any help would be appreciated. Thank 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>
using namespace::std;

//  function will display on cout the contents of an arrary -  FUNCTION 1
void printArray(ostream & out, const string data[], int cellsUsed);


//  function will sort an array                             -  FUNCTION 2
void sortArray(string data[], int cellsUsed);
//   NOTE:  in the string class the assignment operator and all of the relational operators
//                have been overloaded
//          Thus, in your sort algorithm you can use   data[i] < data[j]


void main()
{
	const int CELLS = 5;
	string  names[CELLS] = { "tom", "mary", "ann", "bill", "carol" };

	cout << "Original array" << endl;
	printArray(cout, names, CELLS);
	cout << endl;

	sortArray(names, CELLS);

	cout << "Sorted array" << endl;
	printArray(cout, names, CELLS);
	cout << endl;

	system("pause");
}   // end main

void printArray(ostream & out, const string data[], int cellsUsed)
{
	for (int i = 0; i < cellsUsed; i++)
	{
		out << data[i] << " ";
	}

}

void sortArray(string data[], int cellsUsed)
{
	
}
main must return int.

As far as your problem. Did your professor mention how he wanted it sorted? The most trivial task would be to do bubblesort(brute force). If you're allowed to use already made algorithms you could do something like std::sort(names, names + CELLS); here is an example with part of your code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#include <algorithm>

int main()
{
	const int CELLS = 5;
	std::string  names[CELLS] = { "tom", "mary", "ann", "bill", "carol" };
	
	std::sort(names, names + CELLS);
	
	for(std::string const &name : names)
	    std::cout << name << std::endl;
}
ann
bill
carol
mary
tom
He didn't mention how he wanted it sorted, other than his reference to data[i]<data[j]. It also needs to be within the sort function. The data[i]<data[j] is what threw me.
Sounds like he wants a bubble sort. http://mathbits.com/MathBits/CompSci/Arrays/Bubble.htm
Thanks! I tried to arrange my function around the following but I can't convert the string portion to the temp listed below. The function sort listed is:

void BubbleSort(apvector <int> &num)
{
int i, j, flag = 1; // set flag to 1 to start first pass
int temp; // holding variable
int numLength = num.length( );
for(i = 1; (i <= numLength) && flag; i++)
{
flag = 0;
for (j=0; j < (numLength -1); j++)
{
if (num[j+1] > num[j]) // ascending order simply changes to <
{
temp = num[j]; // swap elements
num[j] = num[j+1];
num[j+1] = temp;
flag = 1; // indicates that a swap occurred.
}
}
}
}
void sortArray(string data[], int cellsUsed)
That shows you the basic algorithm now you just have to make a few changes like change teh vector into the array/size of container. Show me what you come up with, I am not going to write it for you especially without you showing any code.
Thank you, Giblit. Not looking for you to do my homework, just seeking some help. Here's what I have, which isn't working.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void sortArray(string data[], int cellsUsed)
{
int i, j, flag = 1;
int temp; 
int cellsUsed = cellsUsed( ); 
for(i = 1; (i <= cellsUsed) && flag; i++)
{
flag = 0;
for (j=0; j < (cellsUsed -1); j++)
{
if (data[j+1] < data[j]) 
{ 
temp = data[j]; // swap elements
data[j] = data[j+1];
data[j+1] = temp;
flag = 1; 
}
Last edited on
Line 5 I have no idea what that is supposed to be doing I would suggest removing it. Also look closely at line 4; shouldn't that be a std::string and not an int?
Last edited on
Thank you, Giblit. I understand how bubble sorts work, but the coding of it looks foreign to me. Hoping I can get to a point where I understand the coding portion.
Topic archived. No new replies allowed.