diff --git a/curriculum/challenges/_meta/rosetta-code/meta.json b/curriculum/challenges/_meta/rosetta-code/meta.json index 9c123a50fd..3e2c3538a0 100644 --- a/curriculum/challenges/_meta/rosetta-code/meta.json +++ b/curriculum/challenges/_meta/rosetta-code/meta.json @@ -329,8 +329,72 @@ "Sorting algorithms/Bead sort" ], [ - "5a23c84252665b21eecc8002", - "Sorting algorithms/Bogosort" + "5a23c84252665b21eecc8002", + "Sorting algorithms/Bogosort" + ], + [ + "5a23c84252665b21eecc8003", + "Sorting algorithms/Bubble sort" + ], + [ + "5a23c84252665b21eecc8004", + "Sorting algorithms/Cocktail sort" + ], + [ + "5a23c84252665b21eecc8005", + "Sorting algorithms/Comb sort" + ], + [ + "5a23c84252665b21eecc8006", + "Sorting algorithms/Counting sort" + ], + [ + "5a23c84252665b21eecc8007", + "Sorting algorithms/Gnome sort" + ], + [ + "5a23c84252665b21eecc8008", + "Sorting algorithms/Heapsort" + ], + [ + "5a23c84252665b21eecc8009", + "Sorting algorithms/Insertion sort" + ], + [ + "5a23c84252665b21eecc800a", + "Sorting algorithms/Merge sort" + ], + [ + "5a23c84252665b21eecc800b", + "Sorting algorithms/Pancake sort" + ], + [ + "5a23c84252665b21eecc800c", + "Sorting algorithms/Permutation sort" + ], + [ + "5a23c84252665b21eecc800d", + "Sorting algorithms/Quicksort" + ], + [ + "5a23c84252665b21eecc800e", + "Sorting algorithms/Radix sort" + ], + [ + "5a23c84252665b21eecc800f", + "Sorting algorithms/Selection sort" + ], + [ + "5a23c84252665b21eecc8010", + "Sorting algorithms/Shell sort" + ], + [ + "5a23c84252665b21eecc8012", + "Sorting algorithms/Stooge sort" + ], + [ + "5a23c84252665b21eecc8013", + "Sorting algorithms/Strand sort" ], [ "594ecc0d9a8cf816e3340187", diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-bubble-sort.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-bubble-sort.md new file mode 100644 index 0000000000..76d06b50d4 --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-bubble-sort.md @@ -0,0 +1,87 @@ +--- +id: 5a23c84252665b21eecc8003 +title: Sorting algorithms/Bubble sort +challengeType: 5 +--- + +## Description +
+Sort an array of elements using the bubble sort algorithm. The elements must have a total order and the index of the array can be of any discrete type. +The bubble sort is generally considered to be the simplest sorting algorithm. +Because of its simplicity and ease of visualization, it is often taught in introductory computer science courses. +Because of its abysmal O(n2) performance, it is not used often for large (or even medium-sized) datasets. +The bubble sort works by passing sequentially over a list, comparing each value to the one immediately after it. If the first value is greater than the second, their positions are switched. Over a number of passes, at most equal to the number of elements in the list, all of the values drift into their correct positions (large values "bubble" rapidly toward the end, pushing others down around them). Because each pass finds the maximum item and puts it at the end, the portion of the list to be sorted can be reduced at each pass. A boolean variable is used to track whether any changes have been made in the current pass; when a pass completes without changing anything, the algorithm exits. +This can be expressed in pseudo-code as follows (assuming 1-based indexing): +
+repeat
+  hasChanged := false
+  decrement itemCount
+  repeat with index from 1 to itemCount
+    if (item at index) > (item at (index + 1))
+      swap (item at index) with (item at (index + 1))
+      hasChanged := true
+until hasChanged = false
+
+
+ +## Instructions +
+ +
+ +## Tests +
+ +``` yml +tests: + - text: bubblesort should be a function. + testString: assert(typeof bubblesort == 'function', 'bubblesort should be a function.'); + - text: bubblesort([25, 32, 12, 7, 20]) should return a array. + testString: assert(Array.isArray(bubblesort([25, 32, 12, 7, 20])), 'bubblesort([25, 32, 12, 7, 20]) should return a array.'); + - text: bubblesort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32]. + testString: assert.deepEqual(bubblesort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], 'bubblesort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32].'); + - text: bubblesort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45]. + testString: assert.deepEqual(bubblesort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], 'bubblesort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45].'); + - text: bubblesort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43]. + testString: assert.deepEqual(bubblesort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], 'bubblesort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43].'); + - text: bubblesort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38]. + testString: assert.deepEqual(bubblesort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], 'bubblesort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38].'); + - text: bubblesort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48]. + testString: assert.deepEqual(bubblesort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], 'bubblesort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48].'); +``` + +
+ +## Challenge Seed +
+
+ +```js +function bubblesort(arr) { + // Good luck! +} +``` + +
+
+ +## Solution +
+ +```js +function bubblesort (arr) { + var done = false; + while (!done) { + done = true; + for (var i = 1; i arr[i]) { + done = false; + [arr[i-1], arr[i]] = [arr[i], arr[i-1]] + } + } + } + return arr; +} +``` + +
diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-cocktail-sort.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-cocktail-sort.md new file mode 100644 index 0000000000..6bc0f24de8 --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-cocktail-sort.md @@ -0,0 +1,111 @@ +--- +id: 5a23c84252665b21eecc8004 +title: Sorting algorithms/Cocktail sort +challengeType: 5 +--- + +## Description +
+The cocktail shaker sort is an improvement on the Bubble Sort. The improvement is basically that values "bubble" both directions through the array, because on each iteration the cocktail shaker sort bubble sorts once forwards and once backwards. Pseudocode for the algorithm (from wikipedia):

+
+function cocktailSort( A : list of sortable items )
+  do
+    swapped := false
+    for each i in 0 to length( A ) - 2 do
+      if A[ i ] > A[ i+1 ] then // test whether the two
+                                // elements are in the wrong
+                                // order
+        swap( A[ i ], A[ i+1 ] ) // let the two elements
+                                 // change places
+        swapped := true;
+    if swapped = false then
+      // we can exit the outer loop here if no swaps occurred.
+      break do-while loop;
+    swapped := false
+    for each i in length( A ) - 2 down to 0 do
+      if A[ i ] > A[ i+1 ] then
+        swap( A[ i ], A[ i+1 ] )
+        swapped := true;
+  while swapped; // if no elements have been swapped,
+                // then the list is sorted
+
+Write a function that sorts a given array using cocktail sort. +
+ +## Instructions +
+ +
+ +## Tests +
+ +``` yml +tests: + - text: cocktailSort should be a function. + testString: assert(typeof cocktailSort == 'function', 'cocktailSort should be a function.'); + - text: cocktailSort([25, 32, 12, 7, 20]) should return a array. + testString: assert(Array.isArray(cocktailSort([25, 32, 12, 7, 20])), 'cocktailSort([25, 32, 12, 7, 20]) should return a array.'); + - text: cocktailSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32]. + testString: assert.deepEqual(cocktailSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], 'cocktailSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32].'); + - text: cocktailSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45]. + testString: assert.deepEqual(cocktailSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], 'cocktailSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45].'); + - text: cocktailSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43]. + testString: assert.deepEqual(cocktailSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], 'cocktailSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43].'); + - text: cocktailSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38]. + testString: assert.deepEqual(cocktailSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], 'cocktailSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38].'); + - text: cocktailSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48]. + testString: assert.deepEqual(cocktailSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], 'cocktailSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48].'); +``` + +
+ +## Challenge Seed +
+
+ +```js +function cocktailSort (arr) { + // Good luck! +} +``` + +
+
+ +## Solution +
+ +```js +function cocktailSort (arr) { + let isSorted = true; + while (isSorted) { + for (let i = 0; i < arr.length - 1; i++) { + if (arr[i] > arr[i + 1]) { + let temp = arr[i]; + arr[i] = arr[i + 1]; + arr[i + 1] = temp; + isSorted = true; + } + } + + if (!isSorted) + break; + + isSorted = false; + + for (let j = arr.length - 1; j > 0; j--) { + if (arr[j - 1] > arr[j]) { + let temp = arr[j]; + arr[j] = arr[j - 1]; + arr[j - 1] = temp; + isSorted = true; + } + } + } + + return arr; +} +``` + +
diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-comb-sort.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-comb-sort.md new file mode 100644 index 0000000000..1e2827e868 --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-comb-sort.md @@ -0,0 +1,140 @@ +--- +id: 5a23c84252665b21eecc8005 +title: Sorting algorithms/Comb sort +challengeType: 5 +--- + +## Description +
+Implement a comb sort. +The Comb Sort is a variant of the Bubble Sort. +Like the Shell sort, the Comb Sort increases the gap used in comparisons and exchanges. +Dividing the gap by $(1-e^{-\varphi})^{-1} \approx 1.247330950103979$ works best, but 1.3 may be more practical. +Some implementations use the insertion sort once the gap is less than a certain amount. +Also see + +Variants: + +Pseudocode: +
+function combsort(array input)
+  gap := input.size //initialize gap size
+  loop until gap = 1 and swaps = 0
+    //update the gap value for a next comb. Below is an example
+    gap := int(gap / 1.25)
+    if gap < 1 
+      //minimum gap is 1
+      gap := 1
+    end if
+    i := 0
+    swaps := 0 //see Bubble Sort for an explanation
+    //a single "comb" over the input list
+    loop until i + gap >= input.size //see Shell sort for similar idea
+      if input[i] > input[i+gap]
+        swap(input[i], input[i+gap])
+        swaps := 1 // Flag a swap has occurred, so the
+            // list is not guaranteed sorted
+      end if
+      i := i + 1
+    end loop
+  end loop
+end function
+
+Write a function that sorts a given array using Comb sort. +
+ +## Instructions +
+ +
+ +## Tests +
+ +``` yml +tests: + - text: combSort should be a function. + testString: assert(typeof combSort == 'function', 'combSort should be a function.'); + - text: combSort([25, 32, 12, 7, 20]) should return a array. + testString: assert(Array.isArray(combSort([25, 32, 12, 7, 20])), 'combSort([25, 32, 12, 7, 20]) should return a array.'); + - text: combSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32]. + testString: assert.deepEqual(combSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], 'combSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32].'); + - text: combSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45]. + testString: assert.deepEqual(combSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], 'combSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45].'); + - text: combSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43]. + testString: assert.deepEqual(combSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], 'combSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43].'); + - text: combSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38]. + testString: assert.deepEqual(combSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], 'combSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38].'); + - text: combSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48]. + testString: assert.deepEqual(combSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], 'combSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48].'); +``` + +
+ +## Challenge Seed +
+
+ +```js +function combSort (arr) { + // Good luck! +} +``` + +
+
+ +## Solution +
+ +```js +function combSort (arr) { + function is_array_sorted(arr) { + var sorted = true; + for (var i = 0; i < arr.length - 1; i++) { + if (arr[i] > arr[i + 1]) { + sorted = false; + break; + } + } + return sorted; + } + var iteration_count = 0; + var gap = arr.length - 2; + var decrease_factor = 1.25; + + // Until array is not sorted, repeat iterations + while (!is_array_sorted(arr)) { + // If not first gap + if (iteration_count > 0) + // Calculate gap + gap = (gap == 1) ? gap : Math.floor(gap / decrease_factor); + + // Set front and back elements and increment to a gap + var front = 0; + var back = gap; + while (back <= arr.length - 1) { + // If elements are not ordered swap them + if (arr[front] > arr[back]) { + var temp = arr[front]; + arr[front] = arr[back]; + arr[back] = temp; + } + + // Increment and re-run swapping + front += 1; + back += 1; + } + iteration_count += 1; + } + + return arr; +} +``` + +
diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-counting-sort.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-counting-sort.md new file mode 100644 index 0000000000..ef88093722 --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-counting-sort.md @@ -0,0 +1,96 @@ +--- +id: 5a23c84252665b21eecc8006 +title: Sorting algorithms/Counting sort +challengeType: 5 +--- + +## Description +
+Implement the Counting sort. This is a way of sorting integers when the minimum and maximum value are known. +Pseudocode: +
+function countingSort(array, min, max):
+  count: array of (max - min + 1) elements
+  initialize count with 0
+  for each number in array do
+    count[number - min] := count[number - min] + 1
+  done
+  z := 0
+  for i from min to max do
+    while ( count[i - min] > 0 ) do
+      array[z] := i
+      z := z+1
+      count[i - min] := count[i - min] - 1
+    done
+  done
+
+Write a function to implement the above pseudo code. The function should return the sorted array. +
+ +## Instructions +
+ +
+ +## Tests +
+ +``` yml +tests: + - text: countSort should be a function. + testString: assert(typeof countSort == 'function', 'countSort should be a function.'); + - text: countSort([25, 32, 12, 7, 20],7, 32) should return a array. + testString: assert(Array.isArray(countSort([25, 32, 12, 7, 20], 7, 32)), 'countSort([25, 32, 12, 7, 20],7, 32) should return a array.'); + - text: countSort([25, 32, 12, 7, 20],7, 32) should return [7, 12, 20, 25, 32]. + testString: assert.deepEqual(countSort([25, 32, 12, 7, 20], 7, 32), [7, 12, 20, 25, 32], 'countSort([25, 32, 12, 7, 20],7, 32) should return [7, 12, 20, 25, 32].'); + - text: countSort([38, 45, 35, 8, 13],8, 45) should return [8, 13, 35, 38, 45]. + testString: assert.deepEqual(countSort([38, 45, 35, 8, 13], 8, 45), [8, 13, 35, 38, 45], 'countSort([38, 45, 35, 8, 13],8, 45) should return [8, 13, 35, 38, 45].'); + - text: countSort([43, 36, 20, 34, 24],20, 43) should return [20, 24, 34, 36, 43]. + testString: assert.deepEqual(countSort([43, 36, 20, 34, 24], 20, 43), [20, 24, 34, 36, 43], 'countSort([43, 36, 20, 34, 24],20, 43) should return [20, 24, 34, 36, 43].'); + - text: countSort([12, 33, 26, 18, 1, 16, 38],1, 38) should return [1, 12, 16, 18, 26, 33, 38]. + testString: assert.deepEqual(countSort([12, 33, 26, 18, 1, 16, 38], 1, 38), [1, 12, 16, 18, 26, 33, 38], 'countSort([12, 33, 26, 18, 1, 16, 38],1, 38) should return [1, 12, 16, 18, 26, 33, 38].'); + - text: countSort([3, 39, 48, 16, 1, 4, 29],1, 48) should return [1, 3, 4, 16, 29, 39, 48]. + testString: assert.deepEqual(countSort([3, 39, 48, 16, 1, 4, 29], 1, 48), [1, 3, 4, 16, 29, 39, 48], 'countSort([3, 39, 48, 16, 1, 4, 29],1, 48) should return [1, 3, 4, 16, 29, 39, 48].'); +``` + +
+ +## Challenge Seed +
+
+ +```js +function countSort (arr, min, max) { + // Good luck! +} +``` + +
+
+ +## Solution +
+ +```js +function countSort (arr, min, max) { + var i, z = 0, + count = []; + + for (i = min; i <= max; i++) { + count[i] = 0; + } + + for (i = 0; i < arr.length; i++) { + count[arr[i]]++; + } + + for (i = min; i <= max; i++) { + while (count[i]-- > 0) { + arr[z++] = i; + } + } + return arr; +} +``` + +
diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-gnome-sort.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-gnome-sort.md new file mode 100644 index 0000000000..42c0534f1b --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-gnome-sort.md @@ -0,0 +1,93 @@ +--- +id: 5a23c84252665b21eecc8007 +title: Sorting algorithms/Gnome sort +challengeType: 5 +--- + +## Description +
+Gnome sort is a sorting algorithm which is similar to Insertion sort, except that moving an element to its proper place is accomplished by a series of swaps, as in Bubble Sort. +The pseudocode for the algorithm is: +
+function gnomeSort(a[0..size-1])
+  i := 1
+  j := 2
+  while i < size do
+    if a[i-1] <= a[i] then
+      /// for descending sort, use >= for comparison
+      i := j
+      j := j + 1
+    else
+      swap a[i-1] and a[i]
+      i := i - 1
+      if i = 0 then
+        i := j
+        j := j + 1
+      endif
+    endif
+  done
+
+Write a function to implement the above pseudo code. The function should return the sorted array. +
+ +## Instructions +
+ +
+ +## Tests +
+ +``` yml +tests: + - text: gnomeSort should be a function. + testString: assert(typeof gnomeSort == 'function', 'gnomeSort should be a function.'); + - text: gnomeSort([25, 32, 12, 7, 20]) should return a array. + testString: assert(Array.isArray(gnomeSort([25, 32, 12, 7, 20])), 'gnomeSort([25, 32, 12, 7, 20]) should return a array.'); + - text: gnomeSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32]. + testString: assert.deepEqual(gnomeSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], 'gnomeSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32].'); + - text: gnomeSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45]. + testString: assert.deepEqual(gnomeSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], 'gnomeSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45].'); + - text: gnomeSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43]. + testString: assert.deepEqual(gnomeSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], 'gnomeSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43].'); + - text: gnomeSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38]. + testString: assert.deepEqual(gnomeSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], 'gnomeSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38].'); + - text: gnomeSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48]. + testString: assert.deepEqual(gnomeSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], 'gnomeSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48].'); +``` + +
+ +## Challenge Seed +
+
+ +```js +function gnomeSort(a) { + // Good luck! +} +``` + +
+
+ +## Solution +
+ +```js +function gnomeSort(a) { + function moveBack(i) { + for (; i > 0 && a[i - 1] > a[i]; i--) { + var t = a[i]; + a[i] = a[i - 1]; + a[i - 1] = t; + } + } + for (var i = 1; i < a.length; i++) { + if (a[i - 1] > a[i]) moveBack(i); + } + return a; +} +``` + +
diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-heapsort.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-heapsort.md new file mode 100644 index 0000000000..9bef172049 --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-heapsort.md @@ -0,0 +1,146 @@ +--- +id: 5a23c84252665b21eecc8008 +title: Sorting algorithms/Heapsort +challengeType: 5 +--- + +## Description +
+Heapsort is an in-place sorting algorithm with worst case and average complexity of O(n logn). +The basic idea is to turn the array into a binary heap structure, which has the property that it allows efficient retrieval and removal of the maximal element. +We repeatedly "remove" the maximal element from the heap, thus building the sorted list from back to front. +Heapsort requires random access, so can only be used on an array-like data structure. +Pseudocode: +
+function heapSort(a, count) is
+  input: an unordered array a of length count
+ (first place a in max-heap order) + heapify(a, count)
+ end := count - 1 + while end > 0 do + (swap the root(maximum value) of the heap with the + last element of the heap) + swap(a[end], a[0]) + (decrement the size of the heap so that the previous + max value will stay in its proper place) + end := end - 1 + (put the heap back in max-heap order) + siftDown(a, 0, end) +
+
+function heapify(a,count) is
+  (start is assigned the index in a of the last parent node)
+  start := (count - 2) / 2
+ while start ≥ 0 do + (sift down the node at index start to the proper place + such that all nodes below the start index are in heap + order) + siftDown(a, start, count-1) + start := start - 1 + (after sifting down the root all nodes/elements are in heap order)
+function siftDown(a, start, end) is + (end represents the limit of how far down the heap to sift) + root := start
+ while root * 2 + 1 ≤ end do (While the root has at least one child) + child := root * 2 + 1 (root*2+1 points to the left child) + (If the child has a sibling and the child's value is less than its sibling's...) + if child + 1 ≤ end and a[child] < a[child + 1] then + child := child + 1 (... then point to the right child instead) + if a[root] < a[child] then (out of max-heap order) + swap(a[root], a[child]) + root := child (repeat to continue sifting down the child now) + else + return +
+Write a function to sort a collection of integers using heapsort. +
+ +## Instructions +
+ +
+ +## Tests +
+ +``` yml +tests: + - text: heapSort should be a function. + testString: assert(typeof heapSort == 'function', 'heapSort should be a function.'); + - text: heapSort([25, 32, 12, 7, 20]) should return a array. + testString: assert(Array.isArray(heapSort([25, 32, 12, 7, 20])), 'heapSort([25, 32, 12, 7, 20]) should return a array.'); + - text: heapSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32]. + testString: assert.deepEqual(heapSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], 'heapSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32].'); + - text: heapSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45]. + testString: assert.deepEqual(heapSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], 'heapSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45].'); + - text: heapSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43]. + testString: assert.deepEqual(heapSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], 'heapSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43].'); + - text: heapSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38]. + testString: assert.deepEqual(heapSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], 'heapSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38].'); + - text: heapSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48]. + testString: assert.deepEqual(heapSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], 'heapSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48].'); +``` + +
+ +## Challenge Seed +
+
+ +```js +function heapSort (arr) { + // Good luck! +} +``` + +
+
+ +## Solution +
+ +```js +function heapSort (arr) { + function swap(data, i, j) { + var tmp = data[i]; + data[i] = data[j]; + data[j] = tmp; + } + + function put_array_in_heap_order(arr) { + var i; + i = arr.length / 2 - 1; + i = Math.floor(i); + while (i >= 0) { + sift_element_down_heap(arr, i, arr.length); + i -= 1; + } + } + + function sift_element_down_heap(heap, i, max) { + var i_big, c1, c2; + while (i < max) { + i_big = i; + c1 = 2 * i + 1; + c2 = c1 + 1; + if (c1 < max && heap[c1] > heap[i_big]) + i_big = c1; + if (c2 < max && heap[c2] > heap[i_big]) + i_big = c2; + if (i_big == i) return; + swap(heap, i, i_big); + i = i_big; + } + } + put_array_in_heap_order(arr); + var end = arr.length - 1; + while (end > 0) { + swap(arr, 0, end); + sift_element_down_heap(arr, 0, end); + end -= 1 + } + return arr; +} +``` + +
diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-insertion-sort.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-insertion-sort.md new file mode 100644 index 0000000000..4eb3d2fe52 --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-insertion-sort.md @@ -0,0 +1,85 @@ +--- +id: 5a23c84252665b21eecc8009 +title: Sorting algorithms/Insertion sort +challengeType: 5 +--- + +## Description +
+An O(n2) sorting algorithm which moves elements one at a time into the correct position. The algorithm consists of inserting one element at a time into the previously sorted part of the array, moving higher ranked elements up as necessary. To start off, the first (or smallest, or any arbitrary) element of the unsorted array is considered to be the sorted part. +Although insertion sort is an O(n2) algorithm, its simplicity, low overhead, good locality of reference and efficiency make it a good choice in two cases: +(i) small n, +(ii) as the final finishing-off algorithm for O(n logn) algorithms such as mergesort and quicksort. +The algorithm is as follows (from wikipedia): +
+function insertionSort(array A)
+  for i from 1 to length[A]-1 do
+    value := A[i]
+    j := i-1
+    while j >= 0 and A[j] > value do
+      A[j+1] := A[j]
+      j := j-1
+    done
+    A[j+1] = value
+  done
+
+Write a function that performs insertion sort on a given array. The function should return the sorted array. +
+ +## Instructions +
+ +
+ +## Tests +
+ +``` yml +tests: + - text: insertionSort should be a function. + testString: assert(typeof insertionSort == 'function', 'insertionSort should be a function.'); + - text: insertionSort([25, 32, 12, 7, 20]) should return a array. + testString: assert(Array.isArray(insertionSort([25, 32, 12, 7, 20])), 'insertionSort([25, 32, 12, 7, 20]) should return a array.'); + - text: insertionSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32]. + testString: assert.deepEqual(insertionSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], 'insertionSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32].'); + - text: insertionSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45]. + testString: assert.deepEqual(insertionSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], 'insertionSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45].'); + - text: insertionSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43]. + testString: assert.deepEqual(insertionSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], 'insertionSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43].'); + - text: insertionSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38]. + testString: assert.deepEqual(insertionSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], 'insertionSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38].'); + - text: insertionSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48]. + testString: assert.deepEqual(insertionSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], 'insertionSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48].'); +``` + +
+ +## Challenge Seed +
+
+ +```js +function insertionSort (a) { + // Good luck! +} +``` + +
+
+ +## Solution +
+ +```js +function insertionSort (a) { + for (var i = 0; i < a.length; i++) { + var k = a[i]; + for (var j = i; j > 0 && k < a[j - 1]; j--) + a[j] = a[j - 1]; + a[j] = k; + } + return a; +} +``` + +
diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-merge-sort.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-merge-sort.md new file mode 100644 index 0000000000..e56224c181 --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-merge-sort.md @@ -0,0 +1,132 @@ +--- +id: 5a23c84252665b21eecc800a +title: Sorting algorithms/Merge sort +challengeType: 5 +--- + +## Description +
+The merge sort is a recursive sort of order n*log(n). +It is notable for having a worst case and average complexity of O(n*log(n)), and a best case complexity of O(n) (for pre-sorted input). +The basic idea is to split the collection into smaller groups by halving it until the groups only have one element or no elements (which are both entirely sorted groups). +Then merge the groups back together so that their elements are in order. +This is how the algorithm gets its divide and conquer description. +Write a function to sort a collection of integers using the merge sort. The function should return the sorted array. +The merge sort algorithm comes in two parts: a sort function and a merge function +The functions in pseudocode look like this: +
+function mergesort(m)
+  var list left, right, result
+  if length(m) ≤ 1
+    return m
+  else
+    var middle = length(m) / 2
+    for each x in m up to middle - 1
+      add x to left
+    for each x in m at and after middle
+      add x to right
+    left = mergesort(left)
+    right = mergesort(right)
+    if last(left) ≤ first(right)
+      append right to left
+      return left
+    result = merge(left, right)
+    return result
+function merge(left,right) + var list result + while length(left) > 0 and length(right) > 0 + if first(left) ≤ first(right) + append first(left) to result + left = rest(left) + else + append first(right) to result + right = rest(right) + if length(left) > 0 + append rest(left) to result + if length(right) > 0 + append rest(right) to result + return result +
+
+ +## Instructions +
+ +
+ +## Tests +
+ +``` yml +tests: + - text: mergeSort should be a function. + testString: assert(typeof mergeSort == 'function', 'mergeSort should be a function.'); + - text: mergeSort([25, 32, 12, 7, 20]) should return a array. + testString: assert(Array.isArray(mergeSort([25, 32, 12, 7, 20])), 'mergeSort([25, 32, 12, 7, 20]) should return a array.'); + - text: mergeSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32]. + testString: assert.deepEqual(mergeSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], 'mergeSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32].'); + - text: mergeSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45]. + testString: assert.deepEqual(mergeSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], 'mergeSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45].'); + - text: mergeSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43]. + testString: assert.deepEqual(mergeSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], 'mergeSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43].'); + - text: mergeSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38]. + testString: assert.deepEqual(mergeSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], 'mergeSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38].'); + - text: mergeSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48]. + testString: assert.deepEqual(mergeSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], 'mergeSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48].'); +``` + +
+ +## Challenge Seed +
+
+ +```js +function mergeSort (array) { + // Good luck! +} +``` + +
+
+ +## Solution +
+ +```js +function mergeSort (array) { + function merge(left, right, arr) { + var a = 0; + + while (left.length && right.length) { + arr[a++] = (right[0] < left[0]) ? right.shift() : left.shift(); + } + while (left.length) { + arr[a++] = left.shift(); + } + while (right.length) { + arr[a++] = right.shift(); + } + } + + function mergeSortRecurse(arr) { + var len = arr.length; + + if (len === 1) { + return; + } + + var mid = Math.floor(len / 2), + left = arr.slice(0, mid), + right = arr.slice(mid); + + mergeSortRecurse(left); + mergeSortRecurse(right); + merge(left, right, arr); + } + mergeSortRecurse(array) + return array; +} +``` + +
diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-pancake-sort.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-pancake-sort.md new file mode 100644 index 0000000000..0dcdbd5ffd --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-pancake-sort.md @@ -0,0 +1,98 @@ +--- +id: 5a23c84252665b21eecc800b +title: Sorting algorithms/Pancake sort +challengeType: 5 +--- + +## Description +
+Write a function to sort an array of integers (of any convenient size) into ascending order using Pancake sorting. The function should return the sorted array. +In short, instead of individual elements being sorted, the only operation allowed is to "flip" one end of the list, like so: +
+Before:
+6 7 8 9 2 5 3 4 1
+After:
+9 8 7 6 2 5 3 4 1
+
+Only one end of the list can be flipped; this should be the low end, but the high end is okay if it's easier to code or works better, but it must be the same end for the entire solution. (The end flipped can't be arbitrarily changed.) +
+ +## Instructions +
+ +
+ +## Tests +
+ +``` yml +tests: + - text: pancakeSort should be a function. + testString: assert(typeof pancakeSort == 'function', 'pancakeSort should be a function.'); + - text: pancakeSort([25, 32, 12, 7, 20]) should return a array. + testString: assert(Array.isArray(pancakeSort([25, 32, 12, 7, 20])), 'pancakeSort([25, 32, 12, 7, 20]) should return a array.'); + - text: pancakeSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32]. + testString: assert.deepEqual(pancakeSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], 'pancakeSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32].'); + - text: pancakeSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45]. + testString: assert.deepEqual(pancakeSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], 'pancakeSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45].'); + - text: pancakeSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43]. + testString: assert.deepEqual(pancakeSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], 'pancakeSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43].'); + - text: pancakeSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38]. + testString: assert.deepEqual(pancakeSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], 'pancakeSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38].'); + - text: pancakeSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48]. + testString: assert.deepEqual(pancakeSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], 'pancakeSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48].'); +``` + +
+ +## Challenge Seed +
+
+ +```js +function pancakeSort (arr) { + // Good luck! +} +``` + +
+
+ +## Solution +
+ +```js +function pancakeSort(arr) { + for (var i = arr.length - 1; i >= 1; i--) { + // find the index of the largest element not yet sorted + var max_idx = 0; + var max = arr[0]; + for (var j = 1; j <= i; j++) { + if (arr[j] > max) { + max = arr[j]; + max_idx = j; + } + } + + if (max_idx == i) + continue; // element already in place + + var new_slice; + + // flip arr max element to index 0 + if (max_idx > 0) { + new_slice = arr.slice(0, max_idx + 1).reverse(); + for (var j = 0; j <= max_idx; j++) + arr[j] = new_slice[j]; + } + + // then flip the max element to its place + new_slice = arr.slice(0, i + 1).reverse(); + for (var j = 0; j <= i; j++) + arr[j] = new_slice[j]; + } + return arr; +} +``` + +
diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-permutation-sort.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-permutation-sort.md new file mode 100644 index 0000000000..2ec2dc5678 --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-permutation-sort.md @@ -0,0 +1,104 @@ +--- +id: 5a23c84252665b21eecc800c +title: Sorting algorithms/Permutation sort +challengeType: 5 +--- + +## Description +
+Write a function to implement a permutation sort, which proceeds by generating the possible permutations of the input array until discovering the sorted one. The function should return the sorted array. +Pseudocode: +
+while not InOrder(list) do
+  nextPermutation(list)
+done
+
+
+ +## Instructions +
+ +
+ +## Tests +
+ +``` yml +tests: + - text: permutationSort should be a function. + testString: assert(typeof permutationSort == 'function', 'permutationSort should be a function.'); + - text: permutationSort([25, 32, 12, 7, 20]) should return a array. + testString: assert(Array.isArray(permutationSort([25, 32, 12, 7, 20])), 'permutationSort([25, 32, 12, 7, 20]) should return a array.'); + - text: permutationSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32]. + testString: assert.deepEqual(permutationSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], 'permutationSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32].'); + - text: permutationSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45]. + testString: assert.deepEqual(permutationSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], 'permutationSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45].'); + - text: permutationSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43]. + testString: assert.deepEqual(permutationSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], 'permutationSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43].'); + - text: permutationSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38]. + testString: assert.deepEqual(permutationSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], 'permutationSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38].'); + - text: permutationSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48]. + testString: assert.deepEqual(permutationSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], 'permutationSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48].'); +``` + +
+ +## Challenge Seed +
+
+ +```js +function permutationSort (arr) { + // Good luck! +} +``` + +
+
+ +## Solution +
+ +```js +function permutationSort (arr) { + function pSort(a) { + var list = []; + permute(a, a.length, list); + for (var i = 0; i < list.length; i++) { + var x = list[i]; + if (isSorted(x)) + return x; + } + return a; + }; + + function permute(a, n, list) { + if (n === 1) { + var b = a.slice(); + list.push(b); + return; + } + for (var i = 0; i < n; i++) { + swap(a, i, n - 1); + permute(a, n - 1, list); + swap(a, i, n - 1); + }; + }; + + function isSorted(a) { + for (var i = 1; i < a.length; i++) + if (a[i - 1] > a[i]) + return false;; + return true; + }; + + function swap(arr, i, j) { + var temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + }; + return pSort(arr); +} +``` + +
diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-quicksort.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-quicksort.md new file mode 100644 index 0000000000..ce6b98485b --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-quicksort.md @@ -0,0 +1,156 @@ +--- +id: 5a23c84252665b21eecc800d +title: Sorting algorithms/Quicksort +challengeType: 5 +--- + +## Description +
+Write a function to sort an array elements using the quicksort algorithm. The function should return the sorted array. +The elements must have a strict weak order and the index of the array can be of any discrete type. +Quicksort, also known as partition-exchange sort, uses these steps. +
    +
  1. Choose any element of the array to be the pivot.
  2. +
  3. Divide all other elements (except the pivot) into two partitions.
  4. + +
  5. Use recursion to sort both partitions.
  6. +
  7. Join the first sorted partition, the pivot, and the second sorted partition.
  8. +
+The best pivot creates partitions of equal length (or lengths differing by 1). +The worst pivot creates an empty partition (for example, if the pivot is the first or last element of a sorted array). +The run-time of Quicksort ranges from O(n log n) with the best pivots, to O(n2) with the worst pivots, where n is the number of elements in the array. +This is a simple quicksort algorithm, adapted from Wikipedia. +
+function quicksort(array)
+  less, equal, greater := three empty arrays
+  if length(array) > 1
+    pivot := select any element of array
+    for each x in array
+      if x < pivot then add x to less
+      if x = pivot then add x to equal
+      if x > pivot then add x to greater
+    quicksort(less)
+    quicksort(greater)
+    array := concatenate(less, equal, greater)
+
+A better quicksort algorithm works in place, by swapping elements within the array, to avoid the memory allocation of more arrays. +
+function quicksort(array)
+  if length(array) > 1
+    pivot := select any element of array
+    left := first index of array
+    right := last index of array
+    while left ≤ right
+      while array[left] < pivot
+        left := left + 1
+      while array[right] > pivot
+        right := right - 1
+      if left ≤ right
+        swap array[left] with array[right]
+        left := left + 1
+        right := right - 1
+    quicksort(array from first index to right)
+    quicksort(array from left to last index)
+
+Quicksort has a reputation as the fastest sort. Optimized variants of quicksort are common features of many languages and libraries. One often contrasts quicksort with merge sort, because both sorts have an average time of O(n log n). +
+ "On average, mergesort does fewer comparisons than quicksort, so it may be better when complicated comparison routines are used. Mergesort also takes advantage of pre-existing order, so it would be favored for using sort() to merge several sorted arrays. On the other hand, quicksort is often faster for small arrays, and on arrays of a few distinct values, repeated many times."http://perldoc.perl.org/sort.html +
+Quicksort is at one end of the spectrum of divide-and-conquer algorithms, with merge sort at the opposite end. + +With quicksort, every element in the first partition is less than or equal to every element in the second partition. Therefore, the merge phase of quicksort is so trivial that it needs no mention! +
+ +## Instructions +
+ +
+ +## Tests +
+ +``` yml +tests: + - text: quickSort should be a function. + testString: assert(typeof quickSort == 'function', 'quickSort should be a function.'); + - text: quickSort([25, 32, 12, 7, 20]) should return a array. + testString: assert(Array.isArray(quickSort([25, 32, 12, 7, 20])), 'quickSort([25, 32, 12, 7, 20]) should return a array.'); + - text: quickSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32]. + testString: assert.deepEqual(quickSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], 'quickSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32].'); + - text: quickSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45]. + testString: assert.deepEqual(quickSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], 'quickSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45].'); + - text: quickSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43]. + testString: assert.deepEqual(quickSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], 'quickSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43].'); + - text: quickSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38]. + testString: assert.deepEqual(quickSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], 'quickSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38].'); + - text: quickSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48]. + testString: assert.deepEqual(quickSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], 'quickSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48].'); +``` + +
+ +## Challenge Seed +
+
+ +```js +function quickSort (array) { + // Good luck! +} +``` + +
+
+ +## Solution +
+ +```js +function quickSort (array) { + + function swap(i, j) { + var t = array[i]; + array[i] = array[j]; + array[j] = t; + } + + function quicksort(left, right) { + + if (left < right) { + var pivot = array[left + Math.floor((right - left) / 2)], + left_new = left, + right_new = right; + + do { + while (array[left_new] < pivot) { + left_new += 1; + } + while (pivot < array[right_new]) { + right_new -= 1; + } + if (left_new <= right_new) { + swap(left_new, right_new); + left_new += 1; + right_new -= 1; + } + } while (left_new <= right_new); + + quicksort(left, right_new); + quicksort(left_new, right); + + } + } + + quicksort(0, array.length - 1); + + return array; +} +``` + +
diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-radix-sort.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-radix-sort.md new file mode 100644 index 0000000000..386453d39c --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-radix-sort.md @@ -0,0 +1,84 @@ +--- +id: 5a23c84252665b21eecc800e +title: Sorting algorithms/Radix sort +challengeType: 5 +--- + +## Description +
+Write a function to sort an integer array with the radix sort algorithm. The function should return the sorted array. +
+ +## Instructions +
+ +
+ +## Tests +
+ +``` yml +tests: + - text: radixSort should be a function. + testString: assert(typeof radixSort == 'function', 'radixSort should be a function.'); + - text: radixSort([25, 32, 12, 7, 20]) should return a array. + testString: assert(Array.isArray(radixSort([25, 32, 12, 7, 20])), 'radixSort([25, 32, 12, 7, 20]) should return a array.'); + - text: radixSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32]. + testString: assert.deepEqual(radixSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], 'radixSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32].'); + - text: radixSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45]. + testString: assert.deepEqual(radixSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], 'radixSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45].'); + - text: radixSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43]. + testString: assert.deepEqual(radixSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], 'radixSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43].'); + - text: radixSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38]. + testString: assert.deepEqual(radixSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], 'radixSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38].'); + - text: radixSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48]. + testString: assert.deepEqual(radixSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], 'radixSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48].'); +``` + +
+ +## Challenge Seed +
+
+ +```js +function radixSort (old) { + // Good luck! +} +``` + +
+
+ +## Solution +
+ +```js +function radixSort (old) { + for (var shift = 52; shift > -1; shift--) { + var tmp = (function(s) { + var a = []; + while (s-- > 0) + a.push(0); + return a; + })(old.length); + var j = 0; + for (var i = 0; i < old.length; i++) { + var move = old[i] << shift >= 0; + if (shift === 0 ? !move : move) { + tmp[j] = old[i]; + j++; + } else { + old[i - j] = old[i]; + } + }; + for (var i = j; i < tmp.length; i++) { + tmp[i] = old[i - j]; + }; + old = tmp; + }; + return old; +}; +``` + +
diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-selection-sort.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-selection-sort.md new file mode 100644 index 0000000000..3d39ee37aa --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-selection-sort.md @@ -0,0 +1,81 @@ +--- +id: 5a23c84252665b21eecc800f +title: Sorting algorithms/Selection sort +challengeType: 5 +--- + +## Description +
+Write a function to sort an array (or list) of elements using the Selection sort algorithm. The function should return the sorted array. +It works as follows: +First find the smallest element in the array and exchange it with the element in the first position, then find the second smallest element and exchange it with the element in the second position, and continue in this way until the entire array is sorted. +Its asymptotic complexity is O(n2) making it inefficient on large arrays. +Its primary purpose is for when writing data is very expensive (slow) when compared to reading, eg. writing to flash memory or EEPROM. +No other sorting algorithm has less data movement. +
+ +## Instructions +
+ +
+ +## Tests +
+ +``` yml +tests: + - text: selectionSort should be a function. + testString: assert(typeof selectionSort == 'function', 'selectionSort should be a function.'); + - text: selectionSort([25, 32, 12, 7, 20]) should return a array. + testString: assert(Array.isArray(selectionSort([25, 32, 12, 7, 20])), 'selectionSort([25, 32, 12, 7, 20]) should return a array.'); + - text: selectionSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32]. + testString: assert.deepEqual(selectionSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], 'selectionSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32].'); + - text: selectionSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45]. + testString: assert.deepEqual(selectionSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], 'selectionSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45].'); + - text: selectionSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43]. + testString: assert.deepEqual(selectionSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], 'selectionSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43].'); + - text: selectionSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38]. + testString: assert.deepEqual(selectionSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], 'selectionSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38].'); + - text: selectionSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48]. + testString: assert.deepEqual(selectionSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], 'selectionSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48].'); +``` + +
+ +## Challenge Seed +
+
+ +```js +function selectionSort (nums) { + // Good luck! +} +``` + +
+
+ +## Solution +
+ +```js +function selectionSort (nums) { + var len = nums.length; + for(var i = 0; i < len; i++) { + var minAt = i; + for(var j = i + 1; j < len; j++) { + if(nums[j] < nums[minAt]) + minAt = j; + } + + if(minAt != i) { + var temp = nums[i]; + nums[i] = nums[minAt]; + nums[minAt] = temp; + } + } + return nums; +} +``` + +
diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-shell-sort.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-shell-sort.md new file mode 100644 index 0000000000..ff99c2e060 --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-shell-sort.md @@ -0,0 +1,75 @@ +--- +id: 5a23c84252665b21eecc8010 +title: Sorting algorithms/Shell sort +challengeType: 5 +--- + +## Description +
+Write a function to sort an array of elements using the Shell sort algorithm, a diminishing increment sort. The function should return the sorted array. +The Shell sort (also known as Shellsort or Shell's method) is named after its inventor, Donald Shell, who published the algorithm in 1959. +Shell sort is a sequence of interleaved insertion sorts based on an increment sequence. The increment size is reduced after each pass until the increment size is 1. +With an increment size of 1, the sort is a basic insertion sort, but by this time the data is guaranteed to be almost sorted, which is insertion sort's "best case". +Any sequence will sort the data as long as it ends in 1, but some work better than others. +Empirical studies have shown a geometric increment sequence with a ratio of about 2.2 work well in practice. +
+ +## Instructions +
+ +
+ +## Tests +
+ +``` yml +tests: + - text: shellSort should be a function. + testString: assert(typeof shellSort == 'function', 'shellSort should be a function.'); + - text: shellSort([25, 32, 12, 7, 20]) should return a array. + testString: assert(Array.isArray(shellSort([25, 32, 12, 7, 20])), 'shellSort([25, 32, 12, 7, 20]) should return a array.'); + - text: shellSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32]. + testString: assert.deepEqual(shellSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], 'shellSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32].'); + - text: shellSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45]. + testString: assert.deepEqual(shellSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], 'shellSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45].'); + - text: shellSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43]. + testString: assert.deepEqual(shellSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], 'shellSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43].'); + - text: shellSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38]. + testString: assert.deepEqual(shellSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], 'shellSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38].'); + - text: shellSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48]. + testString: assert.deepEqual(shellSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], 'shellSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48].'); +``` + +
+ +## Challenge Seed +
+
+ +```js +function shellSort (a) { + // Good luck! +} +``` + +
+
+ +## Solution +
+ +```js +function shellSort (a) { + for (var h = a.length; h > 0; h = parseInt(h / 2)) { + for (var i = h; i < a.length; i++) { + var k = a[i]; + for (var j = i; j >= h && k < a[j - h]; j -= h) + a[j] = a[j - h]; + a[j] = k; + } + } + return a; +} +``` + +
diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-stooge-sort.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-stooge-sort.md new file mode 100644 index 0000000000..63a394353f --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-stooge-sort.md @@ -0,0 +1,97 @@ +--- +id: 5a23c84252665b21eecc8012 +title: Sorting algorithms/Stooge sort +challengeType: 5 +--- + +## Description +
+Write a function to permform Stooge Sort on an array of integers. The function should return a sorted array. +The Stooge Sort algorithm is as follows: +
+algorithm stoogesort(array L, i = 0, j = length(L)-1)
+  if L[j] < L[i] then
+    L[i]  L[j]
+  if j - i > 1 then
+    t := (j - i + 1)/3
+    stoogesort(L, i , j-t)
+    stoogesort(L, i+t, j )
+    stoogesort(L, i , j-t)
+  return L
+
+
+ +## Instructions +
+ +
+ +## Tests +
+ +``` yml +tests: + - text: stoogeSort should be a function. + testString: assert(typeof stoogeSort == 'function', 'stoogeSort should be a function.'); + - text: stoogeSort([25, 32, 12, 7, 20]) should return a array. + testString: assert(Array.isArray(stoogeSort([25, 32, 12, 7, 20])), 'stoogeSort([25, 32, 12, 7, 20]) should return a array.'); + - text: stoogeSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32]. + testString: assert.deepEqual(stoogeSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], 'stoogeSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32].'); + - text: stoogeSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45]. + testString: assert.deepEqual(stoogeSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], 'stoogeSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45].'); + - text: stoogeSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43]. + testString: assert.deepEqual(stoogeSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], 'stoogeSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43].'); + - text: stoogeSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38]. + testString: assert.deepEqual(stoogeSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], 'stoogeSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38].'); + - text: stoogeSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48]. + testString: assert.deepEqual(stoogeSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], 'stoogeSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48].'); +``` + +
+ +## Challenge Seed +
+
+ +```js +function stoogeSort (arr) { + // Good luck! +} +``` + +
+
+ +## Solution +
+ +```js +function stoogeSort (arr) { + function stoogeSortRecurse(array, i, j) { + if (j === undefined) { + j = array.length - 1; + } + + if (i === undefined) { + i = 0; + } + + if (array[j] < array[i]) { + var aux = array[i]; + array[i] = array[j]; + array[j] = aux; + } + + if (j - i > 1) { + var t = Math.floor((j - i + 1) / 3); + stoogeSortRecurse(array, i, j - t); + stoogeSortRecurse(array, i + t, j); + stoogeSortRecurse(array, i, j - t); + } + } + stoogeSortRecurse(arr); + return arr; +} +``` + +
diff --git a/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-strand-sort.md b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-strand-sort.md new file mode 100644 index 0000000000..d729e7bbf5 --- /dev/null +++ b/curriculum/challenges/english/08-coding-interview-prep/rosetta-code/sorting-algorithms-strand-sort.md @@ -0,0 +1,93 @@ +--- +id: 5a23c84252665b21eecc8013 +title: Sorting algorithms/Strand sort +challengeType: 5 +--- + +## Description +
+Write a function to sort an array using the Strand sort. The function should return the sorted array. +This is a way of sorting numbers by extracting shorter sequences of already sorted numbers from an unsorted list. +
+ +## Instructions +
+ +
+ +## Tests +
+ +``` yml +tests: + - text: strandSort should be a function. + testString: assert(typeof strandSort == 'function', 'strandSort should be a function.'); + - text: strandSort([25, 32, 12, 7, 20]) should return a array. + testString: assert(Array.isArray(strandSort([25, 32, 12, 7, 20])), 'strandSort([25, 32, 12, 7, 20]) should return a array.'); + - text: strandSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32]. + testString: assert.deepEqual(strandSort([25, 32, 12, 7, 20]), [7, 12, 20, 25, 32], 'strandSort([25, 32, 12, 7, 20]) should return [7, 12, 20, 25, 32].'); + - text: strandSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45]. + testString: assert.deepEqual(strandSort([38, 45, 35, 8, 13]), [8, 13, 35, 38, 45], 'strandSort([38, 45, 35, 8, 13]) should return [8, 13, 35, 38, 45].'); + - text: strandSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43]. + testString: assert.deepEqual(strandSort([43, 36, 20, 34, 24]), [20, 24, 34, 36, 43], 'strandSort([43, 36, 20, 34, 24]) should return [20, 24, 34, 36, 43].'); + - text: strandSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38]. + testString: assert.deepEqual(strandSort([12, 33, 26, 18, 1, 16, 38]), [1, 12, 16, 18, 26, 33, 38], 'strandSort([12, 33, 26, 18, 1, 16, 38]) should return [1, 12, 16, 18, 26, 33, 38].'); + - text: strandSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48]. + testString: assert.deepEqual(strandSort([3, 39, 48, 16, 1, 4, 29]), [1, 3, 4, 16, 29, 39, 48], 'strandSort([3, 39, 48, 16, 1, 4, 29]) should return [1, 3, 4, 16, 29, 39, 48].'); +``` + +
+ +## Challenge Seed +
+
+ +```js +function strandSort (list) { + // Good luck! +} +``` + +
+
+ +## Solution +
+ +```js +function strandSort (list) { + + function merge(left, right) { + var result = []; + while (left.length != 0 && right.length != 0) { + if (left[0] <= right[0]) + result.push(left.shift()); + else + result.push(right.shift()); + } + result.push.apply(result, left); + result.push.apply(result, right); + return result; + } + + if (list.length <= 1) return list; + var result = []; + while (list.length > 0) { + var sorted = []; + sorted.push(list.shift()); + var len = list.length; + for (var i = 1; i < len; i++) { + var elem = list[i]; + if (sorted[i - 1] <= elem) { + sorted.push(elem); + sorted.splice(i, 1); + } + } + + result = merge(result, sorted); + } + return result; +} +``` + +