0

I'm trying to call a function recursively (for sorting purposes) but getting an error. I tried to find a solution and found How to call function recursively; I changed my code accordingly, so now it looks like this:

$scope.orderCustom = function() {

    qS.quickSort($scope.mydata, 0, mydata.length-1);
};

var qS = {
    quickSort: function quickSort(data, min, max) {

        if (min < max) {
            var p = qSP.quickSortPartition(data, min, max);
            var newMax = p-1;
            var newMin = p+1;

            qS.quicksort(data, min, newMax);
            qS.quicksort(data, newMin, max);
        }
    }
};

var qSP = {
    quickSortPartition: function quickSortPartition(data, min, max) {

        var pivot = data[max].id;
        var i = min;
        for (var j = min; j <= max; j++) {
            if (data[j].id <= pivot) {
                var pom = data[j];
                data[j] = data[i];
                data[i] = pom;
                i = i+1;
            }
        }  
        var pom = data[i];
        data[i] = data[max];
        data[max] = pom;    

        return i;
    }
};

I'm getting an error "Error: qS.quicksort is not a function". Does anyone know how to solve this problem? I am still not able to leave a comment on stackoverflow, so I had to open a new question for this. Any help would be appriciated. Thanx :)

1
  • Just call quickSort Commented Sep 20, 2016 at 10:16

2 Answers 2

1

The simplest thing would be to get rid of the extra wrapping and just use the functions:

$scope.orderCustom = function() {

    quickSort($scope.mydata, 0, mydata.length-1);
};

function quickSort(data, min, max) {

        if (min < max) {
            var p = quickSortPartition(data, min, max);
            var newMax = p-1;
            var newMin = p+1;

            quickSort(data, min, newMax);
            quickSort(data, newMin, max);
        }
}

function quickSortPartition(data, min, max) {

        var pivot = data[max].id;
        var i = min;
        for (var j = min; j <= max; j++) {
            if (data[j].id <= pivot) {
                var pom = data[j];
                data[j] = data[i];
                data[i] = pom;
                i = i+1;
            }
        }  
        var pom = data[i];
        data[i] = data[max];
        data[max] = pom;    

        return i;
}

Note that you also have to correct the spelling of the recursive call quickSort as you had used all lower case quicksort.

I assume this is just a programming exercise as you would be better for real code to use the built-in sort() function or for angular you might prefer the orderBy filter.

For example, this does the same as you look to be trying to do:

function compare(a,b) {
  if (a.id < b.id)
    return -1;
  if (a.id > b.id)
    return 1;
  return 0;
}

$scope.mydata.sort(compare);
Sign up to request clarification or add additional context in comments.

6 Comments

Aaaah, what a stupid mistake. Sure, the thing was in a typo, quicksort instead of quickSort. Nice catch. Ofcourse it works now, without wrapping (as I initially wrote it). Thank you so much, it would bug me for who knows how long :)
Btw, I can't use orderBy here, that is why I am writing sort by myself (see stackoverflow.com/questions/39572744/…)
@ljerka, you should be able to just call sort with a comparison function, see the edit to my post.
Sorry if this is a stupid question, I am quite new in those stuff... But if I write $scope.mydata.sort(compare), how does compare(a, b) "know" what are my a & b? Btw, what I am trying to sort is a collection that has an id from another collection, and I would like to sort it by a name property from that other collection. So it is not so simple as to compare 2 id-s
a and b are the values the sort function wants to compare, so elements of the array being sorted. If I understand you, you actually want to compare othercollection[a.id].someattribute < othercollection[b.id].someattribute ? It makes the compare function longer but not much harder.
|
0

You don't need to mention qS.

Just quicksort instead of qS.quicksort.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.