Sorting an Array

I have to sort an array of 10000 elements with bubble sort, select sort, insert sort, and quick sort. And print every 1000th term. Im pretty sure there is something wrong with each sort because both insert and quick sort crash the program and bubble and select dont seem to sort them correctly. here is my code so far.

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
#include <iostream>
#include <fstream>

using namespace std;

const int maxs = 10000;

void readem(double a[])
{
	int id;
	ifstream inf;
	inf.open("indata.dat");
	while(!inf.eof())
	{
		for(int i = 0; i <= maxs; i++)
		{
			inf >> a[i];
			i++;
		}
	}
	inf.close();
}
void printem(double a[])
{
	int i;
	ofstream outf;
	outf.open("outdata.dat");
	for(i = 0; i <= maxs; i++)
	{
		switch(i)
		{
			case 1000:
			case 2000:
			case 3000:
			case 4000:
			case 5000:
			case 6000:
			case 7000:
			case 8000:
			case 9000:
			case 10000:
				cout << a[i] << endl;
				break;
		}
	}
}
void bubble(double a[])
{
	int j, l;
	for(l = 0; l <= maxs-1; l++)
	{
		for(j = 0; j >= maxs-1; j++)
		{
			if(a[j] > a[j+1]);
		}
	}
	swap(a[j], a[j+1]);
}
void selection(double a[])
{
	int j, k, small;
	if(maxs > 1)
	{
		for(k = 1; k <= maxs -1; k++)
		{
			small = k;
			for(j = k+1; j <= maxs; j++)
			{
				if(a[j] < a[small]) small = j;
			}
			if(k != small) swap(a[k], a[small]);
		}
	}
}
void insert(double a[])
{
	int j, k, save;
	for(k = maxs -1; k > 1; k++)
	{
		j = k+1;
		save = a[k];
		a[maxs + 1] = save;
		while(save > a[j])
		{
			a[j-1] = a[j];
			j = j + 1;
		}
		a[j-1] = save;
	}
}
void quick(int left, int right)
{
	int j, k;
	double a[maxs];
	if(left < right)
	{
		j = left;
		k = right + 1;
		do
		{
			do j = j + 1; while(a[j] <= a[left]);
			do k = k - 1; while(a[k] >= a[left]);
			if(j < k) swap(a[j], a[k]);
		}
		while(j <= k);
		swap(a[left], a[k]);
		quick(left, k - 1);
		quick(k + 1, right);
	}
}
void main()
{
	double a[maxs + 1];
	int i;
	readem(a);
	for(i = 0; i < 4; i++)
	{
		readem(a);
		switch(i)
		{
			case 0: bubble(a);
					cout << "Bubble Sort" << endl;
					printem(a);
					break;
			case 1: selection(a);
					cout << "Select Sort" << endl;
					printem(a);
					break;
			case 2: insert(a);
					cout << "Insert Sort" << endl;
					printem(a);
					break;
			case 3: quick(0, maxs - 1);
					cout << "Quick Sort" << endl;
					printem(a);
					break;
		}
	}
}
You don't seem to keep track of how many items you actually read.

If the number is less than 10001, you could have uninitialised entries with NaN values.

If the number is greater, you will have overwritten memory somewhere is unpredictable effects.
closed account (o1vk4iN6)
There's so much going wrong here...

1
2
3
4
5
6
void quick(int left, int right)
{
	int j, k;
	double a[maxs]; // what ...
   
        // ... 


And some of your algorithms aren't written correctly...

Your bubble sort does absolutely nothing.
Last edited on
alright i made a few changes to the code that sorted some of the numbers.

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
#include <iostream>
#include <fstream>

using namespace std;

const int maxs = 10000;

void readem(double a[])
{
	int id;
	ifstream inf;
	inf.open("indata.dat");
	while(!inf.eof())
	{
		for(int i = 0; i <= maxs; i++)
		{
			inf >> a[i];
			i++;
		}
	}
	inf.close();
}
void printem(double a[])
{
	int i;
	ofstream outf;
	outf.open("outdatao.dat");
	for(i = 0; i <= maxs; i++)
	{
		switch(i)
		{
			case 1000:
			case 2000:
			case 3000:
			case 4000:
			case 5000:
			case 6000:
			case 7000:
			case 8000:
			case 9000:
			case 10000:
				outf << a[i] << endl;
				break;
		}
	}
}
void bubble(double a[])
{
	int j, l;
	for(l = 0; l <= maxs-1; l++)
	{
		for(j = 0; j <= maxs-1; j++)
		{
			if(a[j] > a[j+1]) swap(a[j], a[j+1]);
		}
	}
}
void selection(double a[])
{
	int j, k, small;
	if(maxs > 1)
	{
		for(k = 1; k <= maxs -1; k++)
		{
			small = k;
			for(j = k+1; j <= maxs; j++)
			{
				if(a[j] < a[small]) small = j;
			}
			if(k != small) swap(a[k], a[small]);
		}
	}
}
void insert(double a[])
{
	int j, k, save;
	for(k = maxs -1; k > 1; k++)
	{
		j = k+1;
		save = a[k];
		a[maxs + 1] = save;
		while(save > a[j])
		{
			a[j-1] = a[j];
			j = j + 1;
		}
		a[j-1] = save;
	}
}
void quick(int left, int right)
{
	int j;
	int k;
	double a[maxs];
	if(left < right)
	{
		j = left;
		k = right + 1;
		do
		{
			do
			{
				j = j + 1;
			}while(a[j] <= a[left]);
			do
			{
				k = k - 1;
			}while(a[k] >= a[left]);
			if(j < k) swap(a[j], a[k]);
		}while(j <= k);
		swap(a[left], a[k]);
		quick(left, k - 1);
		quick(k + 1, right);
	}
}
void main()
{
	double a[maxs + 1];
	int i;
	ofstream outf;
	outf.open("outdatao.dat");
	for(i = 0; i < 4; i++)
	{
		switch(i)
		{
			case 0: readem(a);
					bubble(a);
					outf << "Bubble Sort" << endl;
					printem(a);
					break;
			case 1: readem(a);
					selection(a);
					outf << "Select Sort" << endl;
					printem(a);
					break;
			/*case 2: readem(a);
					insert(a);
					outf << "Insert Sort" << endl;
					printem(a);
					break;
			case 3: readem(a);
					quick(0, maxs - 1);
					outf << "Quick Sort" << endl;
					printem(a);
					break;*/
		}
	}
}


-9.25596e+061
-9.25596e+061
-7686.42
-5384.97
-3020.2
-614.763
1704.47
4149.78
6478.72
8825.16
Hi! I'm not sure of the source of your algorithms, but a good one is Wikipedia. Almost all algorithms you can think of is quasi-coded there. You just need to write them in the language you're tinkering with at the moment, like c++ for instance.
Topic archived. No new replies allowed.