Author Topic: Bubble Sort  (Read 734 times)

0 Members and 1 Guest are viewing this topic.

Offline alias120

  • Subscriber
  • Jr. Member
  • *
  • Posts: 103
  • while(!(succeed = try()));
  • Field: IT Support
  • Real Name: Matthew Balthrop
  • Favorite OS: Windows
  • Programming Language: C++
Bubble Sort
« on: March 02, 2010, 07:01:45 PM »
There are a few tutorials on sorts in here, so this Tutorial seemed appropiate.  Here is an example of the Bubble Sort Algorithm. This is a simple, quick way to sort a set of data into ascending or descending order. This is not the most effective/efficient method of sorting data, but for simple sorts it will do.


Code: C [Select]
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()  
  6. {
  7.  
  8. int array1[] = {10,8,3,7,22};     //Creating an array of five variables
  9. int i = 0;
  10. cout<<"Unsorted array: ";
  11. for(i; i < 5; i++) {
  12. cout<<array1[i]<<", ";               //Display the array
  13. }
  14. cout<<endl;
  15. cout<<endl;
  16.  
  17. int temp;
  18. for (int m = 0; m < 5; m++) {
  19.    for (int n = 0; n < m; n++) {          //So that we can loop through the array five times.
  20.      if (array1[n] > array1[n+1])  {     //We are sorting in ascending order, so if the first variable is greater
  21.      temp = array1[n];                    //than the second the if statement will execute.
  22.      array1[n] = array1[n+1];         //We assign the first variable to a temporary variable,
  23.      array1[n+1] = temp;           //and then swap the second variable to the first position.
  24.      }                                    //We then assign the second position with the temporary variable, and we're done!
  25.     }
  26.    }
  27. cout<<"Sorted array: ";           //Output the sorted array
  28. int j = 0;
  29. for(j = 0; j < 5; j++) {
  30. cout<<array1[j]<<", ";
  31.  }
  32. cout<<endl;
  33. cin.get();
  34. }
  35.  
  36.  
Code^3
--------
"I do not care if you think slowly, but I wish you would think before you publish."
-Wolfgang Pauli

Offline gbertoli3

  • Expert
  • Sr. Member
  • *
  • Posts: 1,169
  • My Code Is In The Cube
    • Bertoli Family
  • Field: Software & Web Development
  • Real Name: Guido Bertoli
  • Favorite OS: Windows
  • Programming Language: C#
Re: Bubble Sort
« Reply #1 on: March 02, 2010, 11:22:44 PM »
EDIT: Sorry didn't see it said not the most effective/efficient way.

For better automation you should make it so you don't have to specify the number of times to loop through.
Your Code: [inline=c++]for (int m = 0; m < 5; m++) {[/inline] or anywhere else that the number 5 shows up.
You should put something like this:[inline=c++]for (int m = 0; m < sizeof(array1)/sizeof(int); m++) {[/inline]

This makes your code much easier to reuse and will work if you don't know how long the length is.

Everything else is pretty good! :^:
« Last Edit: March 02, 2010, 11:25:55 PM by gbertoli3 »

Offline alias120

  • Subscriber
  • Jr. Member
  • *
  • Posts: 103
  • while(!(succeed = try()));
  • Field: IT Support
  • Real Name: Matthew Balthrop
  • Favorite OS: Windows
  • Programming Language: C++
Re: Bubble Sort
« Reply #2 on: March 03, 2010, 02:47:31 PM »
I agree, I've made it a habit of only doing that with dynamically assigned arrays and need to integrate this practice into my statically assigned arrays. Thank you for the critique though, i always appreciate feedback from the experts.

-alias
Code^3
--------
"I do not care if you think slowly, but I wish you would think before you publish."
-Wolfgang Pauli