Bubble sort using recursion in C#

前端 未结 4 713
生来不讨喜
生来不讨喜 2020-12-20 01:09

I\'ve wrote this simple piece of code. And I have a slight problem with it.

int [] x = [50,70,10,12,129];
sort(x, 0,1);
sort(x, 1,2);
sort(x, 2,3);
sort(x, 3         


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-20 01:35

    Nothing wrong with wanting to learn - couple of obvious things.

    Firstly you're already aware that there's a length property for the array - so you could use that to create a loop that gets rid of the multiple calls to sort at the start and makes the length of the array a non problem.

    Secondly you might want to think about the way the sort works - how about this: you're attempting to bubble a value up to its correct place in the list (or down if you prefer!) - so for a list of n items, remove the first, sort the remaining n - 1 items (that's the recursive bit) then bubble the first item into place.

    Been decades since I thought about this, fun!

提交回复
热议问题