Recursive Bubble Sort Question

Nov 23, 2013 at 11:28pm
Is this an acceptable recursive bubble sort? <3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
template<typename T>
void recursiveBubble(T * arr, int size){
	if (bubbleSortAux(arr, size))
		return;
	else
		recursiveBubble(arr, size);
}

template<typename T>
bool bubbleSortAux(T * arr, int size){
	T temp;
	bool sorted = true;
	for (int i = size-1; i >= 0; i--){
		if (arr[i] < arr[i-1]){
			temp = arr[i-1];
			arr[i-1] = arr[i];
			arr[i] = temp;
			sorted = false;
		}
	}
	return sorted;
}


Recursive bubble sort is very inefficient, so why would I want to? I am eventually going to make my algorithm work for linked lists. Low level stuff, perhaps.
Last edited on Nov 23, 2013 at 11:33pm
Nov 23, 2013 at 11:52pm
it's not fully recursive,,
you still use looping ( for )...

learning sorting will probably help you figure out how to compare each element to each other
because that what I think I am trying to do when I learn sorting


Just a hint :
avoid linked list if you perhaps thinking that inserting and deleting in linked list is faster than in vector..

because I used to think when to use linked list or vector..
the answer is always vector
under some very specific condition it's linked list
Nov 24, 2013 at 12:06am
Thanks! I like vectors quite a bit. I will gripe at my professor about the assignment!

Professor wanted us to use recursion for either one of the two bubble sort loops (for either the traditional do-while or the for loop). I chose the first do-while loop to recursify. I was just checking to see if the "recursiveBubble" function is actually recursive, because I'm not actually sure if the "true" bool value here is considered the "base case"? Is it?
Last edited on Nov 24, 2013 at 12:08am
Nov 24, 2013 at 12:08am
You cannot always choose your container. Bubble sort is an excellent choice for handling a linked list. Doing it recursively is not.

If your list is a doubly-linked list, you can even use a bidirectional sort.
http://www.cplusplus.com/faq/sequences/sequencing/sort-algorithms/bubble-sort/

(It still has horrible time characteristics though. If you want speed on linked lists, use a merge sort.)
Topic archived. No new replies allowed.