alphabetical sorting algorithm errors

i need a program that sorts an array in alphabetical order (A->Z). i have made some code that should be working i believe but it doesn't work. can anyone tell me what i am doing wrong ?
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
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <fstream>
#include <string>
#include <time.h>
using namespace std;

int main() {
	clock_t start, end;
	start = clock();
	int sum=0;
	const char * names[]={"MARY","PATRICIA","LINDA","BARBARA","ELIZABETH",
"JENNIFER","MARIA","SUSAN","MARGARET","DOROTHY","LISA",
"NANCY","KAREN","BETTY","HELEN","SANDRA","DONNA",
"CAROL","RUTH","SHARON","MICHELLE"};
        const int length=sizeof(names) / sizeof(*names);
	cout<<length<<endl;
	int values[length];
	string names2[length];
	int count=0;
	int count2=0;
	int count3=0;
	int lowest=0;
	int lowest2[length];
	for (int i=count;i<length;i++) {
		bool cont=true;
		for (int j=0;j<length;j++) {
			if (lowest2[j]==i) {
				cont=false;
				break;
			}
		}
		cout<<cont;
		if (cont) {
		if (strcmp(names[i],names[lowest])>0) {
			lowest=lowest;
		} else {
			lowest=i;
		}
		}
		if (i==length-1) {
			lowest2[count3]=lowest;
			count3++;
			names2[count2]=names[lowest];
			count2++;
			count++;
			i=count;
		}
		
	}
	for (int j=0;j<20;j++) {
	std::cout<<names2[j]<<std::endl;
	}
	end = clock();
	printf("\nTook %f seconds\n", (double)(end-start)/CLOCKS_PER_SEC);
	system("pause");
	return 0;
}

this algorithm is based on finding the lowest value (so for example AARON) store it in the first spot of names2[] and then find the lowest value again etc.. but if it sees AARON again it should skip it because it is already stored. (thats the use of bool cont)
Last edited on
What sort algorithm are you using?

You're using variables before initialising them. Surely your compiler warned you about this, right?
selection sort
and i am not using any variables before initializing
What about lowest2?
ok true lol.
i did a total rework on my code and now i have an error while it is running. i don't see what is going wrong.
here is my new 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
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <fstream>
#include <string>
#include <time.h>
using namespace std;

int main() {
	clock_t start, end;
	start = clock();
	int sum=0;
	const char * names[]={"MARY","PATRICIA","LINDA","BARBARA","ELIZABETH",
"JENNIFER","MARIA","SUSAN","MARGARET","DOROTHY","LISA",
"NANCY","KAREN","BETTY","HELEN","SANDRA","DONNA",
"CAROL","RUTH","SHARON","MICHELLE"};
const int length=sizeof(names) / sizeof(*names);
	cout<<length<<endl;
	int count=0;
	int count2=0;
	int count3=0;
	int lowest=0;
	for (int i=count;i<length;i++) {
		cout<<i<<endl;
		if (strcmp(names[i],names[lowest])>0) {
			lowest=lowest;
		} else {
			lowest=i;
		}
		if (i==length-1) {
			const char * temp=names[count2];
			names[count2]=names[lowest];
			names[lowest]=temp;
			count2++;
                        count3++;
			lowest=count3;//problem was here
			count++;
			i=count;
		}
		
	}
	for (int j=0;j<20;j++) {
	std::cout<<names[j]<<std::endl;
	}
	end= clock();
	printf("\nTook %f seconds\n", (double)(end-start)/CLOCKS_PER_SEC);
	system("pause");
	return 0;
}

EDIT: never mind i changed the code above and it works.
Last edited on
Topic archived. No new replies allowed.