// Exercise 5
//
// Write a bubble sort function which takes a
// std::vector<double> as an argument and returns
// a new vector as a result

#include <iostream>
#include <vector>

using namespace std;

// Bubble sort function
vector<double> bubblesort(vector<double> v) {
  vector<double> vsort = v;
  int vlen = vsort.size() - 1;
  for (int i = vlen; i > 0; i--) {
    for (int j = 0; j < i; j++) {
      // Sorting from largest to smallest
      if (vsort[j] < vsort[j+1]) {
	// Swap these elements
	double tmp = vsort[j];
	vsort[j] = vsort[j+1];
	vsort[j+1] = tmp;
      }
    }
  }
  return vsort;
}

// Test program
int main(int argc, char** argv)
{
  std::vector<double> x(6), y(6);
  // Initialise
  x[0] = 4.3;
  x[1] = 1.2;
  x[2] = 5.7;
  x[3] = 9.1;
  x[4] = 0.2;
  x[5] = 3.3;
  // Sort
  y = bubblesort(x);
  // Print lists
  for (int i = 0; i < x.size(); i++) {
    cout << i << " : " << x[i] << "\t" << y[i] << "\n";
  }
}
