0

My assignment is to make a recursive bubble sort, but I'm not allowed to use any loops.... In fact, the only loop I can use is in the main when I call the function.

I'm totally clueless as to what to do.

This was an example of a bubble sort we worked on in class, I just have no idea how to do it without loops.

#include <iostream>
#include <cmath>
#include <ctime>
using namespace std;

void swap(int& x, int& y) {
  int t = x; x = y; y = t;
}

int main(void) {
  clock_t start,finish;
  double time;
  start = clock();

  const int size = 400000;
  int a[size];
  for(int j = 0; j < size; j++)
    a[j] = rand();
  for(int j = 0; j < size; j++)
    for(int i = 0; i < size - 1 - j; i++)
      if(a[i] > a[i+1])
	swap(a[i], a[i+1]);

  finish = clock();
  time = (double(finish)-double(start))/CLOCKS_PER_SEC;
  cout << "for size = " << size << ", time = " << time << endl;
  return 0;
}

Edited by ajjg123: n/a

2
Contributors
3
Replies
8
Views
7 Years
Discussion Span
Last Post by ajjg123
0

Do you know what is recursive?
Do you know how to implement recursive?

a recursive function is a function that calls itself.

like

bubblesort()
bubblesort()

I have no clue how to implement that into sorting a list of numbers...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.