Thread: Whats wrong with my file streaming/bubble sorting combo?

1. Whats wrong with my file streaming/bubble sorting combo?

The code is self-explanotory. I'm trying to make a program that creates a .txt which contains 10 random integers. Then read the integers and sort it from least to greatest and then print them out in another .txt file.

I used:
File Streaming
Singleton
Bubble Sorting
Class

For this program.

2. In your second for loop, you typed "i < 10" instead of "j < 10".
Also, you used Array[i] instead of Temp in the loop's assignment.

Anyway, I thought you were supposed to bubblesort from opposite ends?

Code:
for(int i= 0; i< 10; i++)
{
for(int j = 10-1; j >= 0; j--)
{
if(Array[j] > Array[i])
{
int Temp= Array[i];
Array[i]= Array[j];
Array[j]= Temp;
}
}

3. I think there are like 4 or 5 different ways to bubble sort. The easiest one that I've found is:
Code:
// sorts from least to greatest
for(int i=0; i<10; i++)
{
for(int j=i+1; j<10; j++)
{
if(Array[j] < Array[i])
{
int Temp= Array[i];
Array[i]= Array[j];
Array[j]= Temp;
}
}

4. Originally posted by PJYelton
I think there are like 4 or 5 different ways to bubble sort. The easiest one that I've found is:
Code:
// sorts from least to greatest
for(int i=0; i<10; i++)
{
for(int j=i+1; j<10; j++)
{
if(Array[j] < Array[i])
{
int Temp= Array[i];
Array[i]= Array[j];
Array[j]= Temp;
}
}
What are the other ways?!

5. Do you have to use a Bubble Sort? I realize you are only sorting 10 integers which is no big deal. However, you should note that Bubble Sort is just about the slowest sorting algorithm imaginable. You could look up Radix Sort or the ever popular Quick Sort if you are interested in others.

6. Originally posted by MrWizard
Do you have to use a Bubble Sort? I realize you are only sorting 10 integers which is no big deal. However, you should note that Bubble Sort is just about the slowest sorting algorithm imaginable. You could look up Radix Sort or the ever popular Quick Sort if you are interested in others.
I don't know what does this has to do with my post that you've quoted?!

>>you are only sorting 10 integers which is no big deal.
It's not me who is sorting it's Alphacentric666.

My post was because PJYelton said that there are about 4 or 5 other ways of bouble sort, and I think there is only one way that is called bubble sort, and any other way of sorting is a different algorithm...

So I think you didn't read the posts well...

7. Other ways to do bubble sort that I can think of off the top of my head - although they are all pretty much the same thing:
Code:
for (int i=0; i<= n; i++)
for(int j=0; j<= n-i-1; j++)
if ( arr[j] > arr[j+1] )
{
int tmp = arr[j+1];
arr[j+1] = arr[j];
arr[j] = tmp;
}
Code:
bool noswap = false;

int i = 0;
while(!noswap)
{
noswap = true;
for (int j = i + 1; j < n; ++j)
{
if (arr[i] > arr[j])
{
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
noswap = false;
}
}
i++;
}
I know there are a couple of others. But like MrWizard said, bubble sort while easy to write is very slow and inefficient. Another favorite of mine beyond the ones MrWizard mentioned is merge sort. Even if it isn't technically the best, I love the idea of an array breaking itself apart and putting itself back together again!

8. [QUOTEI know there are a couple of others. But like MrWizard said, bubble sort while easy to write is very slow and inefficient. Another favorite of mine beyond the ones MrWizard mentioned is merge sort. Even if it isn't technically the best, I love the idea of an array breaking itself apart and putting itself back together again! [/B][/QUOTE]

I see...
Now I understood what you meant by, others ways...

I personally like the QuickSort, it's the best sorting algorithm I've seen.

9. Yep, quicksort rules too! But avoid shell sort...

10. Originally posted by ammar
I don't know what does this has to do with my post that you've quoted?!

>>you are only sorting 10 integers which is no big deal.
It's not me who is sorting it's Alphacentric666.

My post was because PJYelton said that there are about 4 or 5 other ways of bouble sort, and I think there is only one way that is called bubble sort, and any other way of sorting is a different algorithm...

So I think you didn't read the posts well...
Oops, I think I hit quote out of habit. As for the other ways of doing a bubble sort they are all the same except for very MINOR changes. These changes might help a little bit but on the whole they don't help that much.

11. Here's a good Quicksort template I wrote:

Code:
template <class number>
bool greater(number a, number b){
return (a > b);
}
template <class number>
bool less(number a, number b){
return (a < b);
}
//...these are the "internal" templates used by the algorithm...
template <class number>
int partition(number array[], int start, int finish, bool (*compare)(number, number) = greater) {
int pivot = start;
number value = array[pivot];
for( int index = start + 1; index < finish ; ++index )
if( !compare( array[index], value ) )
swap(array[index], array[++pivot]);
swap(array[start], array[pivot]);
return pivot;
}
template <class number>
void quicksort(number array[], int start, int finish, bool (*compare)(number, number) = greater) {
if(start < finish) {
int pivot = partition(array, start, finish, compare);
quicksort(array, start, pivot, compare);
quicksort(array, pivot + 1, finish, compare);
}
return;
}
//...and here is the user template...
template <class number>
void quicksort(number array[], int length, bool (*compare)(number, number) = greater) {
quicksort(array, 0, length, compare);
}

12. Heres an update.

13. Originally posted by Alphacentric666
Heres an update.
Are you still having problems? If so, post questions and only the relevant part of the source within the text of your post. Some people don't read attachments as they usually contain a complete program, which no-one can be bothered to look through.

Popular pages Recent additions