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;
}

Recommended Answers

All 3 Replies

anybody?

anybody?

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

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...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.