Trouble Bubble Sort of book titles

Feb 21, 2017 at 11:20am
I need to make s c++ code of sorting of book titles. 1. display the Inputted book titles. 2. display the book title in Alphabetical order. i was able to do the 1st but I'm having difficulty in displaying the alphabetical order of books. 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
#include"stdafx.h"
#include <iostream>
#include <string>
#include <conio.h>
#include <algorithm>
#include <cstring>
#include <map>
#include<stdlib.h>
#include<time.h>

typedef std::map<char, int> alphabet_t;
using namespace std;

const int MAX = 5;
void BubbleSort(string books, int max);

int main(int argc, const char * argv[])
{
	string books[MAX];

	//inputs
	std::cout << "Enter Book titles: \n";
	for (int i = 0; i < MAX; i++)
	{
		std::cout << "Book [" << i << "]: ";
		//cin >> books[i];
		getline(std::cin, books[i]);

	}
	// print the titles stored in books[i] variable
	cout << "Book Titles Entered \n\n";
	for (int i = 0; i < MAX; i++)
	{
		std::cout << "Book No." << i << ": ";
		cout << books[i] << endl;

	}

	// print the titles after sort
	cout << "Book Titles In Sort Ascending \n\n";
	for (int i = 0; i < MAX; ++i)
		cout << books[i] << "\n";

}
void BubbleSort(string books, int size)
{
	int result;
	for (int pass = 0; pass < size - 1; ++pass)
	{
		for (int i = 0; i < MAX - 1 - pass; ++i)
		{
			result = string (books[i], books[i + 1]);
			if (result > 0) {
				swap(books[i], books[i + 1]);
			}
		}
	}




	system("pause");

}
Feb 21, 2017 at 2:26pm
closed account (E0p9LyTq)
Instead of creating a C-style array to hold your book titles instantiate a std::vector object to hold a varying number of titles. You can then use the sort function in <algorithm>:

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
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

template <typename T>
void DisplayContents(const T& Input);

int main()
{
   std::vector<std::string> biblia;
   biblia.push_back("A Tale of Two Cities");
   biblia.push_back("Tales of An Ancient Empire");
   biblia.push_back("1984");
   biblia.push_back("The Hobbit");
   biblia.push_back("Lord Of The Rings");

   std::cout << "My original library:\n\n";
   DisplayContents(biblia);

   std::sort(biblia.begin(), biblia.end());
   std::cout << "\nMy sorted library:\n\n";
   DisplayContents(biblia);
}


template <typename T>
void DisplayContents(const T& Input)
{
   for (auto iElement : Input)
   {
      std::cout << iElement << '\n';
   }

   std::cout << '\n';
}

My original library:

A Tale of Two Cities
Tales of An Ancient Empire
1984
The Hobbit
Lord Of The Rings


My sorted library:

1984
A Tale of Two Cities
Lord Of The Rings
Tales of An Ancient Empire
The Hobbit

If you want to use/create your own sort algorithm vector elements can be accessed using operator[], same as an array. Just change your function parameter list to pass a reference to a vector, no need to include the vector's size.
Feb 21, 2017 at 2:54pm
I think the problem you are having is you never called your BubbleSort function. So you end up printing out the same list twice.

By the way, what are you doing in line 52? All you need in lines 52-53 is
if (books[i] > books[i+1]) {

Oh, and the first argument to BubbleSort should be string*
Last edited on Feb 21, 2017 at 3:00pm
Feb 21, 2017 at 10:17pm
@FurryGuy
Thanks but how can i do it if i want to manually input the books? i'm not used in using vector. when i encounter this problem the first thing that comes to my mind is using array.
Feb 21, 2017 at 11:02pm
Your BubbleSort() takes a single string as the parameter. It needs to take an array: void BubbleSort(string books[], int size)
Making this change and doug4's change, the code works for me.
Feb 21, 2017 at 11:32pm
@ dhayden

can you paste your working code here... when i change that i get a lots of errors.
Feb 22, 2017 at 4:40am
closed account (E0p9LyTq)
spike32 wrote:
how can i do it if i want to manually input the books?

Simple, write a loop to input the book titles, with a set condition to end the loop.

Good idea is to write a function to read your data, similar to the function to write your data.

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
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

template <typename T>
void GetContents(T& Input);

template <typename T>
void DisplayContents(const T& Input);

int main()
{
   std::vector<std::string> biblia;

   GetContents(biblia);

   std::cout << "\nMy original library (number of books: " << biblia.size() << "):\n\n";
   DisplayContents(biblia);

   std::sort(biblia.begin(), biblia.end());

   std::cout << "\nMy sorted library (number of books: " << biblia.size() << "):\n\n";
   DisplayContents(biblia);
}

template <typename T>
void GetContents(T& Input)
{
   std::string bookName;
 
   std::cout << "Enter a book name (\".\" to quit):\n> ";

   while (std::getline(std::cin, bookName))
   {
      if (bookName == ".")
      {
         break;
      }

      Input.push_back(bookName);
      std::cout << "> ";
   }
}

template <typename T>
void DisplayContents(const T& Input)
{
   for (auto iElement : Input)
   {
      std::cout << iElement << '\n';
   }

   std::cout << '\n';
}

Enter a book name ("." to quit):
> Tales of An Ancient Empire
> A Tale of Two Cities
> 1984
> The Hobbit
> Lord of The Rings
> Learn C++ Vectors
> .

My original library (number of books: 6):

Tales of An Ancient Empire
A Tale of Two Cities
1984
The Hobbit
Lord of The Rings
Learn C++ Vectors


My sorted library (number of books: 6):

1984
A Tale of Two Cities
Learn C++ Vectors
Lord of The Rings
Tales of An Ancient Empire
The Hobbit
Last edited on Feb 22, 2017 at 5:11am
Feb 22, 2017 at 11:52am
Thank You! FurryGuy.. the 2nd code was a very big help but i'm still trying to implement my array because i'm still new in using vector.


Thank you very one!
Feb 22, 2017 at 11:03pm
Strike-throughs show code I deleted. Underlines show code I added or modified.
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
#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h>
#include <algorithm>
#include <cstring>
#include <map>
#include<stdlib.h>
#include<time.h>

typedef std::map<char, int> alphabet_t;
using namespace std;

const int MAX = 5;
void BubbleSort(string books[], int max);

int main(int argc, const char * argv[])
{
	string books[MAX];

	//inputs
	std::cout << "Enter Book titles: \n";
	for (int i = 0; i < MAX; i++)
	{
		std::cout << "Book [" << i << "]: ";
		//cin >> books[i];
		getline(std::cin, books[i]);

	}
	// print the titles stored in books[i] variable
	cout << "Book Titles Entered \n\n";
	for (int i = 0; i < MAX; i++)
	{
		std::cout << "Book No." << i << ": ";
		cout << books[i] << endl;

	}

	BubbleSort(books, MAX);
	
	// print the titles after sort
	cout << "Book Titles In Sort Ascending \n\n";
	for (int i = 0; i < MAX; ++i)
		cout << books[i] << "\n";

}
void BubbleSort(string books[], int size)
{
	for (int pass = 0; pass < size - 1; ++pass)
	{
		for (int i = 0; i < MAX - 1 - pass; ++i)
		{
			result = string (books[i], books[i + 1])
			if (books[i] > books[i+1]) {
				swap(books[i], books[i + 1]);
			}
		}
	}




	system("pause");

}

Feb 28, 2017 at 11:57pm
@FurryGuy (1237)
is it possible to put 2 While here because i need to repeat it again after inputting and sorting several books.
Topic archived. No new replies allowed.