-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Viktor Lesyk
committed
Sep 14, 2019
1 parent
d6b41f4
commit 870706d
Showing
4 changed files
with
268 additions
and
0 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
Algorithms/Sorting/MergeSort/MergeSort.playground/Contents.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
func mergeSort(numbers: [Int]) -> [Int] { | ||
// If only one element - already sorted. | ||
if numbers.count == 1 { | ||
return numbers | ||
} | ||
|
||
// First, divide the list into equal-sized sublists | ||
// consisting of the first half and second half of the list. | ||
let iMiddle = numbers.count/2 | ||
let left = mergeSort(numbers: Array(numbers[0..<iMiddle])) | ||
let right = mergeSort(numbers: Array(numbers[iMiddle..<numbers.count])) | ||
|
||
// Recursively sort both sublists. | ||
return compareAndMerge(left: left, right: right) | ||
} | ||
|
||
func compareAndMerge(left: [Int], right:[Int]) -> [Int] { | ||
var leftIndex = 0 | ||
var rightIndex = 0 | ||
var ordered: [Int] = [] | ||
|
||
while leftIndex < left.count && rightIndex < right.count { | ||
if left[leftIndex] < right[rightIndex] { | ||
ordered.append(left[leftIndex]) | ||
leftIndex += 1 | ||
} else { | ||
ordered.append(right[rightIndex]) | ||
rightIndex += 1 | ||
} | ||
} | ||
|
||
// Going through leftovers | ||
ordered += Array(left[leftIndex..<left.count]) | ||
ordered += Array(right[rightIndex..<right.count]) | ||
|
||
return ordered | ||
} | ||
|
||
var numbers = [5, 15, 14, 1, 26, 0, 99]; | ||
|
||
print(mergeSort(numbers: numbers)) |
4 changes: 4 additions & 0 deletions
4
Algorithms/Sorting/MergeSort/MergeSort.playground/contents.xcplayground
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<playground version='5.0' target-platform='ios'> | ||
<timeline fileName='timeline.xctimeline'/> | ||
</playground> |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
function mergeSort(numbers: number[]): number[] { | ||
|
||
// If only one element - already sorted. | ||
if (numbers.length === 1) { | ||
return numbers; | ||
} | ||
|
||
// First, divide the list into equal-sized sublists | ||
// consisting of the first half and second half of the list. | ||
const iMiddle = Math.floor(numbers.length/2); | ||
|
||
const leftArray = []; | ||
numbers.forEach((el, index) => { | ||
if (0 <= index && index < iMiddle) { | ||
leftArray.push(el); | ||
} | ||
}); | ||
|
||
const rightArray = []; | ||
numbers.forEach((el, index) => { | ||
if (iMiddle <= index && index <= numbers.length) { | ||
rightArray.push(el); | ||
} | ||
}); | ||
|
||
const left = mergeSort(leftArray); | ||
const right = mergeSort(rightArray); | ||
|
||
// Recursively sort both sublists. | ||
return compareAndMerge(left, right); | ||
} | ||
|
||
function compareAndMerge(left: number[], right: number[]): number[] { | ||
let ordered = []; | ||
let leftIndex = 0; | ||
let rightIndex = 0; | ||
|
||
while (leftIndex < left.length && rightIndex < right.length) { | ||
if (left[leftIndex] < right[rightIndex]) { | ||
ordered.push(left[leftIndex]); | ||
leftIndex++; | ||
} else { | ||
ordered.push(right[rightIndex]); | ||
rightIndex++; | ||
} | ||
} | ||
|
||
// Going through leftovers | ||
left.forEach((el, index) => { | ||
if (leftIndex <= index && index <= left.length) { | ||
ordered.push(el); | ||
} | ||
}); | ||
|
||
right.forEach((el, index) => { | ||
if (rightIndex <= index && index <= right.length) { | ||
ordered.push(el); | ||
} | ||
}); | ||
|
||
return ordered; | ||
} | ||
|
||
const unsortedArrayOfNumbers = [5, 15, 14, 1, 26, 0, 99]; | ||
console.log(mergeSort(unsortedArrayOfNumbers)); |
This file contains 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