insertion sort printing out strange values

I created an insertion sort and it runs alright but instead of printing out the numbers I entered it prints out a strange string of numbers that make no sense.
my program will work like this when I'm done
a)you enter the size of data/array first
ex. 10 integers

b)then you enter a random sequence of numbers either manually or have the program generate the numbers.

this is my code for that and it really doesn't work like i want it yet
any suggestions on how I can do this better?
1
2
3
4
5
6
srand(time(0));

cout << "\nGenerating " << n << " random integers..." << endl;
	for (int e = 0; e < n; e++)
	{
   a[e] = rand() % 100; //range: [0, 99] 


c)after the numbers are entered then you are prompt to select a method of sorting
ex. insertion, quick, merge, etc.

d) then the program will return the sorted data
my program is returning numbers like
-336860191-33686019-33686019
i entered three simple numbers and got three long strings of gibberish like illustrated above. why am I getting this as a result?

here's my 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
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
//CMP425
//Theory of Algorithms
//Dr. Alak
//Jason Thomas
//Multiple Sorts

#include <iostream>
#include <algorithm>
#include <iterator>
#include <functional> 

using namespace std;
	
		void insertionSort(int a[], int n, int e){
		int i, j;
		for (i=1; i<=sizeof(a[e]); ++i){
			int key = a[i];
			for(j=i-1; j>=0 && a[j]> key; --j)
				a[j+1]=a[j];
				a[j+1]=key;
				cout<<a[j];
				}
	}



int main()
{


char sortSelection;
char enterValues;

int e=0;
//rand (time(0));
cout<<"Please enter size of array: ";
int n;
cin>>n; 
cout<<endl;
int *a = new int[n];
cout<<"Enter a random series of integers: "<<endl;
cout<<"Press (M) to enter them manually"<<endl;
cout<<"Press (R) to automatically generate the values"<<endl;
cin>>enterValues;
if(enterValues == 'M'||'m')
{
	cout<<"enter by keyboard: "<<endl;
for (int e=0; e<n; e++)
{
  cin>>a[e];
}
cout<<endl;
cout<<"Choose which method of sorting you would like to do "<<endl;
cout<<"For Merge Sort select (M or m) For Insertion Sort select (I or i) "<<endl;
cout<<"For Selection Sort select (S or s) For Quick Sort select (Q or q) "<<endl;
cin>>sortSelection;

				if (sortSelection == 'I'||'i')
					{
					
					 insertionSort(a, n, e);
					 
					}
			
				if (sortSelection == 'S'||'s')
					{
					
					
				    // selection_sort (a, n, e);
					}
			
				if (sortSelection == 'M'||'m')
					{
					 // mergesort();		
					}
}
else
if (enterValues == 'R'||'r')
   {
	cout << "\nGenerating " << n << " random integers..." << endl;
	for (int e = 0; e < n; e++)
	{
   a[e] = rand() % 100; //range: [0, 99]
}
cout<<endl;
cout<<"Choose which method of sorting you would like to do "<<endl;
cout<<"For Merge Sort select (M or m) For Insertion Sort select (I or i) "<<endl;
cout<<"For Selection Sort select (S or s) For Quick Sort select (Q or q) "<<endl;
cin>>sortSelection;

				if (sortSelection == 'I'||'i')
					{
					
					 insertionSort(a, n, e);
					 
					}
			
				if (sortSelection == 'S'||'s')
					{
					
					
				    // selection_sort (a, n, e);
					}
			
				if (sortSelection == 'M'||'m')
					{
					 // mergesort();		
					}
}
				
	system ("PAUSE");
	return 0;
}



I'd appreciate any and all sorts of advice here
what ever I can get
Thank you
Last edited on
That sizeof(a[e]) thing does not get the size of the array. It gets the size in bytes of an element of the array, which is not what you want as the loop limit in your for loop.

Also, in your if statements, conditions like sortSelection == 'M'||'m' checks, as the second part of the condition, whether 'm' is true. Since 'm' is non-zero, it is always true and that code will always execute, which I assume is not what you want.
thanks Zhuge
is that the reason I'm getting the strange numbers?
it's driving me crazy that I cant figure this out.
I know my Algorithms are good its just that stuff in the main I gotta figure out.
and should I change sortSelection == 'M'||'m' to
sortSelection == 'M'&&'m' ?
I've added to my code with some more sorts and I changed that sizeof thing also. here's what I've added.

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
//CMP425
//Theory of Algorithms
//Dr. Alak
//Jason Thomas
//Multiple Sorts

#include <iostream>
#include <algorithm>
#include <iterator>
#include <functional> 

using namespace std;
typedef int data;
const int eMax = 100;
	
void insertionSort(int a[], int e)
	{
		for (int i=1; i<= e; i++)
		{
			int key;
			key = a[i];
			for(int j=i-1; j>=0 && a[j]> key; j--)
			{
				a[j+1]=a[j];
				a[j+1]=key;
				cout<<a[j];
			}
				
	   }
	}


void selectionSort(int a[], int e){
	int i, min;
	  for (i=0; i<e; i++){
	   
	    for (int j = i, min=i; j<e; j++){
	         if (a[j]<a[min])
	            min=j;
	      }
          int temp=a[i];
          a[i]=a[min];
          a[min]=temp;
      }
}

void quickSort(int *a, int e){
	if (e<2)
		return;
	int pivot = a[e/2];
	int *left = a;
	int *right = a+e-1;
	while (left<=right){
		if (*left < pivot){
			left++;
			continue;
		}
		if(*right > pivot) {
			right--;
			continue;
		}
		int temp = *left;
		*left++ = *right;
		*right-- = temp;
	}
	quickSort(a, right - a+1);
	quickSort(left, a + e - left);
}
int main()
{


char sortSelection;
char enterValues;

int e=0;
//rand (time(0));
cout<<"Please enter size of array: ";
int n;
cin>>n; 
cout<<endl;
int *a = new int[n];
cout<<"Enter a random series of integers: "<<endl;
cout<<"Press (M) to enter them manually"<<endl;
cout<<"Press (R) to automatically generate the values"<<endl;
cin>>enterValues;
if(enterValues == 'M'||'m')
{
	cout<<"enter by keyboard: "<<endl;
for (int e=0; e<n; e++)
{
  cin>>a[e];
}
cout<<endl;
cout<<"Choose which method of sorting you would like to do "<<endl;
cout<<"For Merge Sort select (M or m) For Insertion Sort select (I or i) "<<endl;
cout<<"For Selection Sort select (S or s) For Quick Sort select (Q or q) "<<endl;
cin>>sortSelection;

				if (sortSelection == 'I'||'i')
					 {
					  a[e];
					  int e = sizeof a/sizeof a[0];
					  insertionSort(a, e);
					 }
			
				if (sortSelection == 'S'||'s')
					{
						a[e];
						int e = sizeof a/sizeof a[0];
						selectionSort (a, e);
					}
			
				if (sortSelection == 'M'||'m')
					{
					 // mergeSort();		
					}
				if (sortSelection == 'Q'||'q')
				{
					a[e];
					int e = sizeof a/sizeof a[0];
					quickSort(a, e);
				}
}
else
if (enterValues == 'R'||'r')
   {
	cout << "\nGenerating " << n << " random integers..." << endl;
	for (int e = 0; e < n; e++)
	{
   a[e] = rand() % 100; //range: [0, 99]
}
	cout<<endl;
cout<<"Choose which method of sorting you would like to do "<<endl;
cout<<"For Merge Sort select (M or m) For Insertion Sort select (I or i) "<<endl;
cout<<"For Selection Sort select (S or s) For Quick Sort select (Q or q) "<<endl;
cin>>sortSelection;

				if (sortSelection == 'I'||'i')
					{
						a[e];
					 insertionSort(a, e);
					 
					}
			
				if (sortSelection == 'S'||'s')
					{
						a[e];
					selectionSort (a, e);
					}
			
				if (sortSelection == 'M'||'m')
					{
						a[e];
					 // mergesort(a, e);		
					}
				if (sortSelection == 'Q'||'q')
				{
					a[e];
					quickSort(a, e);
				}
}
				
	system ("PAUSE");
	return 0;
}

I still need to fix that random part
Last edited on
Topic archived. No new replies allowed.