This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // var numbers: [Int] = [15, 67, 8, 16, 44, 27, 12, 35]; | |
| // We will call quicksort with | |
| // quickSort(&numbers, p: 0, r: numbers.count-1); | |
| func quickSort(_ numbers: inout [Int], p: Int, r: Int) -> Void{ | |
| if p<r { | |
| let q = partition(&numbers, p: p, r: r) | |
| quickSort(&numbers, p: p, r: q-1); | |
| quickSort(&numbers, p: q+1, r: r) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // We will call quicksort with | |
| // quickSort(array, 0, len(array)-1) | |
| func quickSort(A []int, p int, r int) []int { | |
| if p < r { | |
| var q = partition(A, p, r) | |
| quickSort(A, p, q-1) | |
| quickSort(A, q+1, r) | |
| } | |
| return A |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // We will call quicksort with p: 0 and r: Array.length - 1 | |
| // console.log(quickSort(Array, 0, B.length - 1)); | |
| function quickSort(A: number[], p: number, r: number): number[] { | |
| if (p < r) { | |
| const q = partition(A, p, r); | |
| quickSort(A, p, q - 1); | |
| quickSort(A, q + 1, r); | |
| } | |
| return A; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // We will call the function with quicksort(numbers, 1, numbers.length - 1) | |
| int partition(int *numbers, int p, int r){ | |
| int x = numbers[r]; | |
| int i = p - 1; | |
| for (int j = p; j<r; j++) { | |
| if(numbers[j]<=x){ | |
| i = i + 1; | |
| swap(numbers[i], numbers[j]); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // numbers = array of numbers | |
| // size = length of numbers | |
| void insertionSort(int *numbers,int size){ | |
| int i, j, key; | |
| for (j=1; j<size; j++) { | |
| key = numbers[j]; | |
| i = j - 1; | |
| while(i>=0 and numbers[i]>key){ | |
| numbers[i+1] = numbers[i]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func insertionSort(A []int) []int { | |
| for j := 0; j < len(A); j++ { | |
| var key = A[j] | |
| var i = j - 1 | |
| for i >= 0 && A[i] > key { | |
| A[i+1] = A[i] | |
| i = i - 1 | |
| } | |
| A[i+1] = key |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function insertionSort(A: number[]): number[] { | |
| for (let j = 1; j < A.length; j++) { | |
| let key = A[j]; | |
| let i = j - 1; | |
| while (i >= 0 && A[i] > key) { | |
| A[i + 1] = A[i]; | |
| i = i - 1; | |
| } | |
| A[i + 1] = key; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function selectionSort(A: number[]): number[] { | |
| for (let i = 0; i < A.length - 1; i++) { | |
| let min = i; | |
| for (let j = i + 1; j < A.length; j++) { | |
| if (A[j] < A[min]) { | |
| min = j; | |
| } | |
| } | |
| [A[min], A[i]] = [A[i], A[min]] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func selectionSort(A []int) []int { | |
| for i := 0; i < len(A)-1; i++ { | |
| var min = i | |
| for j := i + 1; j < len(A); j++ { | |
| if A[j] < A[min] { | |
| min = j | |
| } | |
| } | |
| A[min], A[i] = A[i], A[min] | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func bubbleSort(A []int) []int { | |
| for i := 1; i < len(A); i++ { | |
| for j := len(A) - 1; j >= i; j-- { | |
| if A[j-1] > A[j] { | |
| A[j-1], A[j] = A[j], A[j-1] | |
| } | |
| } | |
| } | |
| return A |
NewerOlder