Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var arr = [5, 56, 33, 54, 37, 67, 84, 38, 44, 43, 94, 49];
- //Bubble
- for (i = 0; i < arr.length; i++) {
- for (j = 0 ; j < (arr.length - i); j++) {
- if (arr[j] > arr[j+1]) {
- temp = arr[j];
- arr[j] = arr[j+1];
- arr[j+1] = temp;
- }
- }
- }
- console.log(arr);
- // Insertion
- for (i = 0; i < arr.length; i++) {
- for (j = i ; j >= 0; j--) {
- if (arr[j] > arr[j+1]) {
- temp = arr[j];
- arr[j] = arr[j+1];
- arr[j+1] = temp;
- }
- }
- }
- console.log(arr);
- // Selection
- for (i = 0; i < arr.length; i++) {
- min = i;
- for (j = i+1 ; j < arr.length ; j++) {
- if (arr[j] < arr[min]) {
- min = j;
- }
- }
- temp = arr[i];
- arr[i] = arr[min];
- arr[min] = temp;
- }
- console.log(arr);
- // Merge
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement