Help with bubblesort program

I need to create a bubble sort program that will read a text file with 100 integers in it, bubble sort it, and output a text file with it sorted as a 10x10 tabular form and on the bottom it has to specify how many passes it took to sort out the numbers.

Right now I am simply just trying to work on the basic level of just getting the program to grab the text file, read the integers, sort them, and then print them out in a new file.
However, when the program runs it doesn't print it out in a 10x10 form. On top of that I can't get it to go through my bubble sort function or get it to write to a file at the moment so I'm just trying to figure out how to read the text file, send it to a 2 dimensional array, and print it out.

I can't find out what to do and I've searched extensively about sorting and grabbing files.

This is what I have so far:

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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;

void bubbsort(int arr[]);
int main(){
	char file[100];
	const int a=10;
	const int b=10;
	int x=0, y=0;
	int arr[a][b];
	cout << "Please enter the name of the file: " << endl;
	cin >> file;
	ifstream data(file);
	if (data.is_open()){
		for (int i=0; i<10; i++){
			for (int j=0; j<10; j++){
				data >> arr[i][j];
				cout << arr[i][j] << ", ";
			}
		}
	}
	data.close();
	system("pause");
	return 0;
}


void bubbsort (int arr[]){
	for (int i=0; i<=99; i++){
		for (int j=i+1; j<=100; j++){
			int temp;
			if(arr[i]>arr[j]){
				temp=arr[i];
				arr[i]=arr[j];
				arr[j]=temp;
			}
		}
	}
}


Thanks for looking.
Last edited on
You need to output a newline character for every 10 values you print out.

1
2
3
4
5
6
7
for (int i=0; i<10; i++){
    for (int j=0; j<10; j++){
        data >> arr[i][j];
        cout << arr[i][j] << ", ";
    }
    cout << endl;
}


You don't need to put the numbers in a grid to print them out in a grid. You can use a 1-d array (which suits your bubblesort function) and calculate the appropriate one-dimensional index to print out with i and j like so:
cout << arr[i * 10 + j].
Thanks for your response. I tried putting what you suggested in there but it didn't work, but I did change it back to a single array with a new line added on to the output. I also switched back to a single dimensional array and I passed it through the bubble sort, but I got a lot of garbage values, my text file has this:
100 94 59 83 7 11 92 76 37 89 74 59 65 79 49 89 89 75 64 82 15 74 82 68 92 61 33 95 91 82 89 64 43 93 86 65 72 40 42 90 81 62 90 89 35 81 48 33 94 81 76 86 67 70 100 80 83 78 96 58

and my updated code looks like this:
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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
using namespace std;

void bubbsort(int arr[]);

int main(){
	char file[100];
	const int a=100;
	int count=0;
	string line;
	const int b=10;
	int z=100;
	int arr[a];
	cout << "Please enter the name of the file: " << endl;
	cin >> file;
	ifstream data(file);
	if (data.is_open()){
	char j='\n';
		for (int i=0; i<100; i++){
			data >> arr[i];
			bubbsort (arr);
			cout << arr[i] <<'\n';
		}
	}
	data.close();
	system("pause");
	return 0;
}


void bubbsort (int arr[]){
	for (int i=0; i<=99; i++){
		for (int j=i+1; j<=100; j++){
			int temp;
			if(arr[i]>arr[j]){
				temp=arr[i];
				arr[i]=arr[j];
				arr[j]=temp;
			}
		}
	}
}


Also at the end when the program finishes it tells me that there is an error that the stack around variable 'arr' was corrupted. I'm loosing hair because of this :(
Last edited on
With a help of a buddy of mine we were able to figure out how to get it done. In case anyone needs a reference of something similar to this I'll post our results:
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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstdlib>
#include <vector>
using namespace std;

void bubbsort(int arr[]);

int main(){
	string file;
	const int a=100, b=10, c=10;
	int count=0, count1=0, d=0, swap=0;
	int clam[a]={0}, ray[b][c]={0};
	cout << "Please enter the name of the file: " << endl;
	cin >> file;
	ifstream data;
	data.open(file);
	vector<int> array;
	int number;
	while(data >> number) {
    array.push_back(number);
	count++;
	d=count;
	clam[count];
	}
	data.close();
	data.open(file);
	while (data.good()){
		int i, j;
			for (i=0; i<d; i++){
				data >> clam[i];
					cout << clam[i]<<" ";
					count1++;
			}      
		}
	cout << endl << "There is/are " << d << " integer/s in the " << '"' << file << '"' << " file." << endl;
	data.close();
	ofstream newfile("newfile.txt");
	data.open(file);
	for (int k=0; k<=count-1; k++){
                for (int l=k+1;l<=count-1; l++){
                        int temp=0;
                        if(clam[k]>clam[l]){
                                temp=clam[k];
                                clam[k]=clam[l];
                                clam[l]=temp;
								swap++;
						}
                }
				newfile << clam[k] << " ";
	}
	cout << endl << "The bubble sort perfomed " << swap << " amount of swaps" << endl;
	cout << "The new sorted text will be saved as " << '"' << "finale.txt" << '"' <<  " in the default directory." << endl;
	data.close();
	cout << endl;
	ofstream finalfile("finale.txt");
	finalfile << "The file " << file << " was sorted via the bubble sort" << endl << "The amount of swaps performed: " << swap << endl;
	finalfile << "The amount of integers: " << count << endl << endl << "Sorted list: " << endl << endl;
	for(int y=0; y<count;y++){
		for(int z=0; z<=9; z++){
			if (y!=count){
			cout<<right<<setw(4)<<clam[y];
            finalfile<<right<<setw(4)<<clam[y];
			y++;
			}
			else {
			cout << endl;
			finalfile << endl;
			system("pause");
			return 0;
			}
		}
         y=y-1;
         cout<<endl;
         finalfile<<endl;
         }
	system("pause");
	return 0;
}
Last edited on
Topic archived. No new replies allowed.