bubble up sort in array

I am trying to sort an array using a bubble up sort, I can't get the sorted array to print. Yes I know a bubble up sort isn't the best sort but it is what I'm most comfortable with.
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
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <new>
using namespace std;
#define Length 150
ifstream in;

int Anumber[Length];

int main()
{
	string file;
	
	cout << "Please enter the location of the file" << endl;
	cout << "An example would be C:/my file.txt" << endl;
	getline(cin, file);
	
	in.open(file);
	if (!in)
	{
		cerr << "Error file not found" << endl;
		exit(1);
	}
	if (in.eof())
	{
		cerr << "The file is blank, The program will now exit" << endl;
		exit(1);
	}
	if (in.is_open())
	{
		cout << "The file opended, and here is the file data" << endl;
		
	}

	int A, X;
	A = Length;
	for (A = 0; A < Length; A++)
	{
		in >> Anumber[A];
		X = Anumber[A];
		cout << X << endl;
	}
	system("pause");
	
	cout << endl;
	
	cout << "The numbers sorted smallest to largest are " << endl;
	
	cout << endl;
	
	for (A = 0; A < Length - 1; A++)
	{
		for (A = X + 1; X <= Length; X++)
		{
			int temp = -1;

			if (Anumber[X] > Anumber[A])
			{
				temp = Anumber[A];
				Anumber[A] = Anumber[X];
				Anumber[X] = temp;
				
			}
		}
	}
		for (A = 0; A <= Length; A++)
		{
	
		cout << Anumber[A] << endl;
		}
	

	
	return 0;
};
If you are not required to write your own sorting algorithm, just use std::sort.
http://www.cplusplus.com/reference/algorithm/sort/
http://en.cppreference.com/w/cpp/algorithm/sort
Last edited on
That sorts it, I don't understand the code, but it does sort it.
You don't have to understand how std::sort works to use it - that's the beauty of generic programming. All you need to know is how to use it, and you don't need to worry about the details.

Do you have any further questions for this topic?
could you tell me where I went wrong on the bubble sort? or am I too far off?
On line 55 you wrote for(A = X + 1; instead of for(X = A + 1; which caused an infinite loop.
Last edited on
Thanks, for the feed back.
Topic archived. No new replies allowed.