0

I've created my own custom quicksort routine to work with a custom data structure I have. It should work just like a regular quicksort except that for comparisons I need use a special function to convert strings into numeric values.

Anyways I must have done something wrong because Firefox tells me "error too much recursion".

Here is the code:

//Will be called on various buckets to sort by dates
function target_sort_wrapper(array) {
    target_sort(array, array.length, 0, length-1);
}

//Quicksort to swap around targets based on dates
//"array" is DDATA, where DDATA[i] are targets
function target_sort(array, length, left, right) {
    if (length < 2) return;
    var pivotIndex = choosePivot(array, length); //returns the index
    partition(array, pivotIndex, left, right);
    //recursive calls now - left then right
    target_sort(array, pivotIndex, 0, pivotIndex - 1);
    target_sort(array, array.length - (pivotIndex+1), pivotIndex+1, array.length - 1);
}

function partition(array, pivotIndex, left, right) {
    //first, put the pivot as the first element to make things easier
    swap(array, pivotIndex, 0);
    var pivot = array[0];
    var i = left + 1; 
    for(var j = left + 1; j < right; j++) {
        //if (array[j] > pivot) { } //do nothing, satisfies invariant
        if (dateValue(array[j].date) < dateValue(pivot.date)) {
            swap(array, i, j);
            i = i + 1; 
        }
    }
}

function choosePivot(array, length) {
    return Math.floor(Math.random() * length); //0 (inclusive) to length (exclusive) 
}

function swap(arr, i, j) {
    var temp = arr[i];
    arr[i] = arr[j];
    arr[j] = temp;
}

Thanks for any help.

JDS
  • 16,388
  • 47
  • 161
  • 224
  • 3
    I don't mean to be rude, but by the time you get to your 143rd question one would think that the process of correctly formatting code would be pretty familiar. – Pointy Jul 23 '12 at 21:06
  • could you please format it so that it's easier to read? hint: http://jsfiddle.net – Yoeri Jul 23 '12 at 21:06
  • Why do you have a random pivot? Why you can't use the default Array's sort function? – davidbuzatto Jul 23 '12 at 21:08
  • 1
    @davidbuzatto: Using a [random pivot](http://en.wikipedia.org/wiki/Quicksort#Choice_of_pivot) is an acceptable method that helps protect against pathological cases like pre-sorted data. – mellamokb Jul 23 '12 at 21:09
  • I don't get it how do you format code so easily? – JDS Jul 23 '12 at 21:11
  • Where is the "dateValue" definition? – Victor Ribeiro da Silva Eloy Jul 23 '12 at 21:12
  • It's not relevant, it's just what I use for comparisons. It has been tested and works though. – JDS Jul 23 '12 at 21:14
  • Right so I understand the formatting was messed up but why the downvotes? I'm looking for some help on a legitimate piece of code. – JDS Jul 23 '12 at 21:16
  • I would like to know why you can't use the sort() function of Array. – davidbuzatto Jul 23 '12 at 21:18
  • @YoungMoney: Try http://jsbeautifier.org/. However, you should always do the same by hand when writing your code. – Bergi Jul 23 '12 at 21:19
  • Because I'm sorting objects by a particular string property (their date), which is in the format "Jul23", "Oct02", etc. I need a different API for comparing. And what's wrong with trying to write a good ole' quicksort now and again? – JDS Jul 23 '12 at 21:19
  • No, you dont need. Just use a sorting function. I will post an answer with an example. – davidbuzatto Jul 23 '12 at 21:22
  • You have not presented any good test cases, and that may be preventing you from catching the problem effectively. What does you function do when given an array of length zero? – dyoo Jul 23 '12 at 21:26
  • @dyoo you're right, so I tried changing my base case to "length < 2" but the problem persists. In terms of test cases, just consider an array of objects that have a "date" property. – JDS Jul 23 '12 at 21:30
  • 1
    Your recursive calls are not correct. But you knew that already. :). Strongly suggest looking at http://en.wikipedia.org/wiki/Quicksort. In particular, the first recursive call isn't using 'left', and it probably should. The second recursive call isn't properly computing the length of the chunk to sort, nor is it properly using left or right. – dyoo Jul 23 '12 at 22:50

1 Answers1

1

To do a custom sort, you can use a "compare function". Take a look:

Although you have a good question, you don't need to implement your sorting algorithm or be concerned about it, just use what I said.

Community
  • 1
  • 1
davidbuzatto
  • 9,207
  • 1
  • 43
  • 50
  • Thanks for the response, upvoted. However my main interest here is actually the implementation of quicksort, I'm sad to say I've never really done it. So I was hoping to figure out what's wrong with my logic. – JDS Jul 23 '12 at 21:32
  • @YoungMoney: ok, no problem. I thought that your problem was just to sort the array. See ya! – davidbuzatto Jul 23 '12 at 21:34