bubble and selection sorting using dynamic array

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

using namespace std;


void bubbleSort(string *names, int &i);
void selectionSort (string *names, int &i);

 int main()
{
        // Variables
    
         string * names = NULL;
         int i;
         char choice;
    
        // Input & Processing
    
    cout << " This program displays array elements in order." << endl;
    cout << " How many names will you enter? : ";
    cin >> i;
    
    names = new string[i];
   
 for (int count= 0; count < i; count++)
    {
        cout << " Enter name: ";
        cin >> names[i];
        
    }
    
    names = names - (i -1);
    
    cout << " Which sorting algorithm will be used (Enter B for Bubble sort or S for Selection sort): ";
    cin >> choice;
    //if ( choice == 'B')
    //{
    
    bubbleSort(names, i);
        
        for (int count= 0; count < i; count++)
        {
            cout << *names;
            names++;
            
        }

        
    //}
    //else if ( choice == 'S')
    //{
        //selectionSort(names, i);
        //for (int count= 0; count < i; count++)
        //{
            //cout << *names;
            //names++;
            
        //}

        
    //}

}
void bubbleSort(string *names, int &i)
{
    bool swap;
    string temp;
    
    do
    {
        swap = false;
        for (int count = 0; count < (i - 1); count++)
        {
            if (names[count] > names [count+1])
            {
                temp = names[count];
                names[count] = names[count+1];
                names[count + 1] = temp;
                swap = true;
                
            }
            
        }
        
    } while(swap);
    
}

void selectionSort (string *names, int &i)
{
    int start_scan, min_index;
    string min_value;
    
    for (start_scan = 0; start_scan < (i - 1); start_scan++)
    {
        
        min_index = start_scan;
        min_value = names[start_scan];
        
        for (int index = start_scan + 1; index < i; index++)
        {
            if (names[index]<min_value)
            {
                min_value = names[index];
                min_index = index;
                
            }
            
        }
        names[min_index] = names[start_scan];
        names[start_scan] = min_value;
        
    }
    
}



This runs but once I try to use the bubble or selection functions it doesn't work. I commented out the selection function to work on one at a time but I'm at a loss.
Last edited on
Please edit your post to contain code tags and intuitive indentation. Code in plain text is tedious to read.
See http://www.cplusplus.com/articles/jEywvCM9/
Better?
Yes. What do you do on line 32?
Topic archived. No new replies allowed.