Quantcast
Channel: Braden the Software Guy » Sorting and Searching
Viewing all articles
Browse latest Browse all 4

C++ – Bubble Sort

$
0
0

Daily Program for 3/22/2013

Today we will be tackling one of the most common computer science algorithms, the bubble sort. For some, this algorithm gives terrible flashbacks to undergraduate computer science courses and to others represents the anti-thesis of what should be taught in a modern computer science classroom. However, it is the simplest of the common data structure sorting algorithms and is a lot of fun to implement. Essentially, bubble sort works by comparing pairs of elements in the array up to the n-1th element and swaps the elements in the pair if the second element is greater than the first.

Problem Statement for Bubble Sort:

Take as input a series of unordered integers and define a function which takes as input an unordered array (or vector or list) and returns a sorted array (or vector or list) of the integers in ascending order.

Code:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> bubbleSort(vector<int> vec) {
   int sorted;
   while (sorted != 0) {
      sorted = 0;
      for (vector::iterator i = vec.begin(); i != vec.end(); i++) {
         if (i + 1 != vec.end()) {
            if (*i > *(i + 1)) {
               iter_swap(i, i + 1);
               sorted += 1;
            }
         }
      }
   }
   return vec;
}

int main(int argc, const char *argv[]) {
   int i, j;
   vector avec;
   for (i = 0; i < 5; i++) {
      cout << "Element " << i + 1 << ": ";
      cin << j;
      avec.push_back(j);
   }
   avec = bubbleSort(avec);
   cout << "\nThe sorted vector is as follows: " << endl;
   for (vector::iterator it = aver.begin(); it != aver.end(); it++) {
      cout << *it << endl;
   }
   return 0;
}

Explanation:

In our bubble sort function our overall goal is to loop through all of the elements in the vector until all of the elements are in ascending order. We accomplish this by having an outer while loop which runs through all of the elements with the loop invariant that stipulates that the number of changes, denoted by sorted, is zero. We use an inner loop to iterate through the elements by using an iterator starting with the 0th element and ending at the n – 1th element. We compare sequential values and swap them if the second value is greater than the first. Additionally, to track whether or not we can claim that the vector is not sorted we add a value of one to our sorted integer variable if a change has been made. As a side note, you may be wondering why I decided to use a call by value when passing the vector to the bubble sort function only to have it return to the same variable. Well, the simple answer is that we will be using this function again in the future where it may be nice to persist a copy of the unsorted vector.

Until next time! Happy programming!



Viewing all articles
Browse latest Browse all 4

Trending Articles