1 Replies - 12853 Views - Last Post: 01 November 2008 - 11:13 PM Rate Topic: -----

#1 ibaraku  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 190
  • Joined: 12-May 07

Recursive selection sort

Posted 01 November 2008 - 06:39 PM

I am having problems seeing why this is not sorting, the code works, it runs fine, but it doesn't sort, I can't seem to find the problem, any help is appreciated...

#include<iostream>
using namespace std;

void selsort(int arg1[], int arg1);

int lengthOfArray = 0;

int main() {

	const int total_size = 10;
	int array[total_size] = {100, 34, 290, 1, -4, -90, 28, 300, 2, 4};
	int length = 0;

	cout << "Initial array...\n";

	for(int j = 0; j < total_size; j++) {
		cout << array[j] << " ";
		lengthOfArray++;
	}
	cout << "******LENGTH IS: ******** " << length <<endl;
	cout << "\nCalling selsort()..." <<endl;

	selsort(array, length);

	for(int k = 0; k < total_size; k++) {
		cout << array[k] << " ";
	}

	return 0;

}

void selsort(int arg0[], int arg1) {

	if(arg1 >= lengthOfArray - 1) {
		return;
	}//end if

	int min_index;
	min_index = arg1;
	for(int index = arg1 + 1; index < lengthOfArray; index++) {
		if(arg0[index] < arg0[min_index]) {
			min_index = index;
		}//end if
	}//end for

	int temp = arg0[arg1];
	arg0[arg1] = arg0[min_index];
	arg0[arg1] = temp;

	selsort(arg0, arg1 + 1);

}




Is This A Good Question/Topic? 0
  • +

Replies To: Recursive selection sort

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4399
  • View blog
  • Posts: 12,253
  • Joined: 18-April 07

Re: Recursive selection sort

Posted 01 November 2008 - 11:13 PM

Well typically a selection sort that is using recursion has three parameters, the array, the start and the stop... but you can do it your way as well with these minor changes...

void selsort(int arg0[], int arg1) {
	// Check to make sure argument is less than total length of array
	// So remove the arg1 >= length of array - 1
	// Keep in mind that also no return, we stop when the if fails.
	if (arg1 < lengthOfArray) {
		int min_index;
		min_index = arg1;
		for(int index = arg1 + 1; index < lengthOfArray; index++) {
			if(arg0[index] < arg0[min_index]) {
				min_index = index;
			}//end if
		}//end for

		int temp = arg0[arg1];
		arg0[arg1] = arg0[min_index];

		// Error here, needed min_index as the subscript
		arg0[min_index] = temp; 

		selsort(arg0, arg1 + 1);
	}
}



You had some logic problems in your code as well as a syntax error. Remember we want to do this sort only if arg1 is less than the length of the array. Your other way it immediately returned because lengthOfArray - 1 would always be 10 <= -1 which is false and would immediately return.

Notice that it is also void, so no return keyword needed here. We keep going with the recursion until finally arg1 is greater than the array.

Lastly you need to have min_index as the subscript, not arg1.

This will get your sort working. Enjoy!

"At DIC we be selection sorting code ninjas... we select only the cool ninjas and then we sort them into deadly teams of destruction!" :snap:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1