Weirdness in simple sorting code

I am working on sorting codes, and planning compare them,.. but i m facing really weird problem, could you please help?

After the insertion sort is done, i m storing the sorted array in a "sorted[]" array (line 62). (to compare later with other algos), but in the for loop, where i m dumping 'elements[]' into 'sorted[]', if i comment out that line, then it shows that the insertion sort works proper, but if i keep it, the 'elements' array i m printing shows that the sorting din't go well.

Is this some kind of optimization problem? i m using xcode.

Thank you very much!

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
#include <iostream>
#include <time.h>

using namespace std;

int main(int argc, char **argv){

    int n, *numbers, *elements, *sorted, flag = 0;

    cout << "Enter number of elements to be sorted: " << endl;

    while(!(cin >> n)){
        cout << "Please enter a number!: " << endl;
        cin.clear();
        cin.ignore(1000, '\n');
    }

    cout << "The number you entered is: " << n;
    numbers = new int(n);
    elements = new int(n);
    sorted = new int[n]();

    srand(time(NULL));
    for(int i = 0; i < n; i++){
        numbers[i] = rand() % n +1;
    }

    cout << "\nThe elements are: " << endl;
    for(int i = 0; i < n; i++)
        cout << numbers[i] << '\t';

    cout << "\nThese numbers will be sorted by following algorithms:" << endl
    << "1. Insertion sort" << endl
    << "2. Selection sort" << endl
    << "3. Bubble sort" << endl
    << "4. Merge sort" << endl
    << "5. Quick sort" << endl;

 ///////////////////////////////////////////Insertion Sort/////////////////////////////////////////////////

    for(int i = 0; i < n; i++){
        elements[i] = numbers[i];
        cout << elements[i] << '\t';
    }
    cout << endl;

    for(int i = 1; i < n; i++){
        for(int j = i; j > 0; j--){
            if(elements[j] < elements[j-1]){
                int temp = elements[j];
                elements[j] = elements[j-1];
                elements[j-1] = temp;
            }
            else break;
        }
    }


    cout << "Sorted array is: " << endl;
    for (int i = 0 ; i<n; i++) {
        cout << elements[i] << '\t';
        sorted[i] = elements[i];
    }
    cout << endl;
    for(int i = 0; i < n; i++)
        cout << sorted[i] << '\t';
    cout << endl;
 ///////////////////////////////////////////Selection Sort////////////////////////////////////////////////////

    for(int i = 0; i < n; i++){
        elements[i] = numbers[i];
        cout << elements[i] << '\t';
    }
    cout << endl;

    for(int i = 0; i < n-1; i++){
        int min = i;
        for(int j = i+1; j < n; j++){
        if(elements[min] > elements[j])
            min = j;
        }

        if(min != i){
            int temp = elements[min];
            elements[min] = elements[i];
            elements[i] = temp;
        }
    }

    for(int i = 0; i < n; i++)
        cout << elements[i] << '\t';

    for(int i = 0; i < n; i++)
        if(elements[i] != sorted[i]){
            flag = 1;
            cout << "\nMismatch occured at i = " << i << " with " << elements[i] << " not equal to " << sorted[i] << endl;
        }
    if(flag == 1){
        cout << "Sorted elements don't match!" << endl;
        flag = 0;
    }
    else {
        cout << "Sorted elements match!" << endl;
    }



    delete[] numbers;
    delete[] sorted;
    delete[] elements;

    return 0;
}
Topic archived. No new replies allowed.