sorting algorithms in javascript code example
Example 1: basic sorting algorithms c#
public static void IntArrayBubbleSort (int[] data)
{
int i, j;
int N = data.Length;
for (j=N-1; j>0; j--) {
for (i=0; i<j; i++) {
if (data [i] > data [i + 1])
exchange (data, i, i + 1);
}
}
}
Example 2: what is the algorithm for javascript array.sort
function bubbleSort(arr){
var len = arr.length;
for (var i = len-1; i>=0; i--){
for(var j = 1; j<=i; j++){
if(arr[j-1]>arr[j]){
var temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
bubbleSort([7,5,2,4,3,9]);
bubbleSort([9,7,5,4,3,1]);
bubbleSort([1,2,3,4,5,6]);
Example 3: sorting in python
python list sort()