bubble sort issues

I having trouble with my bubble sort in my sorting function(sort_array) Am not sure if its a syntax error or its logic it just that every time i run It will display half my program.

#include <string>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <iomanip>
using namespace std;
void print_array(string *names_arr, int arr_size);
void sort_array(string *names_arr, int arr_size);
int main()
{
srand(time(NULL));
string inst_names[] = { "Amy", "Tim", "Sue", "Dave", "Maya", "Carol", "Katy", "Paula", "Paul", "Kane",
"Denise", "Keith"};

//********************** 5 points *******************
//code a range based for loop to print out the contents of the inst_names array
for (auto name : inst_names)
{
cout << name << " ";

}

//generate a random number between 5 and 10 and place it in arr_size
int arr_size = rand() % 5 + 10;

string *names_arr = new string [arr_size];

for (int i = 0; i < arr_size; i++)
{
names_arr[i] = inst_names[i];
}






//create a string array with arr_size elements and place address of this newly created
//array into the names_arr pointer variable. Populate this newly created
//array ffrom elements in the inst_names array
//********************** 5 points *******************



cout << "\n---Before sort---\n";

//**************** 15 points for print_arr function **************
print_array(names_arr, arr_size);


//The print_array function prints the elements in the array whose address
//is maintained in the names_arr pointer
//Remember to create a function prototype for the function

sort_array(names_arr, arr_size);
//**************** 15 points for sort_arr function **************
//The sort_array function must sort the elements in the array whose address
//is maintained in the names_arr pointer
//Remember to create a function prototype for the function!

cout << "---After sort---\n";
print_array(names_arr, arr_size);

delete [] names_arr;
//delete the memory taken up by the array whose address is maintained in the names_arr pointer
//**************** 10 points deletion statement **************
system("pause");
return 0;
}
void print_array(string *names_arr, int arr_size)
{
cout << "array size = " << arr_size << endl;
for (int j = 0; j < arr_size; j++)
{
cout << names_arr[j] << ' ';
}
}
void sort_array(string *names_arr, int arr_size)
{
for (int passnum = 0; passnum < (arr_size + 1); passnum++)
{
for (int k = 0; k <= arr_size + 1; k++)
{
if (names_arr[k] > names_arr[k + 1])
{
swap(names_arr[k], names_arr[k + 1]);
}
}

}

}
Sincerely, what's wrong in using the "code" tags?
Anyway, as far as I can see you don't have the swap() function. Or perhaps you forgot to copy&paste it?
Also, this statement
1
2
//generate a random number between 5 and 10 and place it in arr_size
int arr_size = rand() % 5 + 10;

generates a number between 10 to 14.
Last edited on
Please use code tags.

std::swap is in <algorithm>, so OK.

Your main problems are the array bounds that you are exceeding in your sort_array() routine.

The code below works (I think!) with minimal changes to your code. However, you can improve the bubble sort as the passes get successively shorter (as one end of the array will be increasingly sorted) and you could also stop once you get a pass with no swaps.

Also, it just takes the first arr_size elements from your master array - but I leave that for you to sort out.

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
#include <string>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <iomanip>
using namespace std;
void print_array(string *names_arr, int arr_size);
void sort_array(string *names_arr, int arr_size);

int main()
{
   srand(time(NULL));
   string inst_names[] = { "Amy", "Tim", "Sue", "Dave", "Maya", "Carol", "Katy", "Paula", "Paul", "Kane",
                           "Denise", "Keith"};

   for (auto name : inst_names)
   {
      cout << name << " ";
   }

   //generate a random number between 5 and 10 and place it in arr_size
   int arr_size = 5 + rand() % 6;              // <===== sort out the limits

   string *names_arr = new string [arr_size];

   for (int i = 0; i < arr_size; i++)
   {
      names_arr[i] = inst_names[i];
   }

   cout << "\n---Before sort---";
   print_array(names_arr, arr_size);

   sort_array(names_arr, arr_size);

   cout << "\n---After sort---";
   print_array(names_arr, arr_size);

   delete [] names_arr;
}

void print_array(string *names_arr, int arr_size)
{
   cout << "array size = " << arr_size << endl;
   for (int j = 0; j < arr_size; j++)
   {
      cout << names_arr[j] << ' ';
   }
}

void sort_array(string *names_arr, int arr_size)
{
   for (int passnum = 0; passnum < arr_size; passnum++)   // <==== watch limits
   {
      for (int k = 0; k < arr_size - 1; k++)              // <==== watch limits - you will need k+1
      {
         if (names_arr[k] > names_arr[k + 1])
         {
            swap(names_arr[k], names_arr[k + 1]);
         }
      }
   }
}
Last edited on
std::swap is in <algorithm>, so OK.

Oooops! Terrible bloop! :$
Thank you for your correction, @lastchance.
Last edited on
Topic archived. No new replies allowed.