为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode912.排序数组 | Sort an Array

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/10886335.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Given an array of integers nums, sort the array in ascending order. 

Example 1:

Input: [5,2,3,1]
Output: [1,2,3,5]

Example 2:

Input: [5,1,1,2,0,0]
Output: [0,0,1,1,2,5] 

Note:

  1. 1 <= A.length <= 10000
  2. -50000 <= A[i] <= 50000

给定一个整数数组 nums,将该数组升序排列。 

示例 1:

输入:[5,2,3,1]
输出:[1,2,3,5]

示例 2:

输入:[5,1,1,2,0,0]
输出:[0,0,1,1,2,5] 

提示:

  1. 1 <= A.length <= 10000
  2. -50000 <= A[i] <= 50000

160ms

 1 class Solution {
 2     func sortArray(_ nums: [Int]) -> [Int] {
 3         var nums = nums
 4         quickSort(&nums, 0, nums.count - 1)
 5         return nums
 6     }
 7     func quickSort(_ nums: inout [Int], _ startIndex: Int, _ endIndex: Int) {
 8         if startIndex >= endIndex { return }
 9         let k = partition(&nums, startIndex, endIndex)
10         quickSort(&nums, startIndex, k - 1)
11         quickSort(&nums, k + 1, endIndex)
12     }
13     func partition(_ nums: inout [Int], _ startIndex: Int, _ endIndex: Int) -> Int {
14         if nums.count == 0 || startIndex >= endIndex {
15             return startIndex
16         }
17         //print("nums:\(nums),s:\(startIndex), e:\(endIndex)")
18         let pivot = nums[startIndex]
19         
20         var lo = startIndex + 1
21         var hi = endIndex
22         while true {
23             while nums[lo] <= pivot && lo < endIndex {
24                 lo += 1
25             }
26             while nums[hi] > pivot {
27                 hi -= 1
28             }
29             //print("lo:\(lo), hi:\(hi)")
30             if lo < hi {
31                 (nums[lo], nums[hi]) =  (nums[hi], nums[lo])
32             } else {
33                 (nums[startIndex], nums[hi]) =  (nums[hi], nums[startIndex]) 
34                 return hi
35             }
36         }
37     }
38 }

164ms

 1 class Solution {
 2     func sortArray(_ nums: [Int]) -> [Int] {        
 3         var array = Array(repeating: 0, count: 100_001)
 4         
 5         for n in nums {
 6             array[n &+ 50_000] += 1
 7         }        
 8         var output = [Int]()
 9         output.reserveCapacity(nums.count)
10         
11         for x in 0..<100_001 {
12             for _ in 0..<array[x] {
13                 output.append(x &- 50_000)
14             }
15         }        
16         return output
17     }
18 }

168ms

 1 class Solution {
 2     func sortArray(_ nums: [Int]) -> [Int] {
 3         if nums.count <= 1 {
 4             return nums
 5         }
 6         
 7         var numsCopy = [Int](nums)
 8         var helper = [Int](nums)
 9         mergeSort(
10            nums: &numsCopy,
11            helper: &helper,
12            leftIndex: 0,
13            rightIndex: nums.count - 1
14         )
15         
16         return numsCopy
17     }
18     
19     private func mergeSort (
20         nums: inout [Int], 
21         helper: inout [Int], 
22         leftIndex: Int, 
23         rightIndex: Int
24     ) {
25         if leftIndex >= rightIndex {
26             return
27         }
28         
29         let midIndex = leftIndex + (rightIndex - leftIndex) / 2
30         
31         mergeSort(
32             nums: &nums, 
33             helper: &helper, 
34             leftIndex: leftIndex, 
35             rightIndex: midIndex
36         )
37         mergeSort(
38             nums: &nums, 
39             helper: &helper, 
40             leftIndex: midIndex + 1,                     
41             rightIndex: rightIndex
42         )
43         
44         merge(
45             nums: &nums,
46             helper: &helper,
47             leftIndex: leftIndex,
48             rightIndex: rightIndex,
49             midIndex: midIndex
50         )
51     } 
52     
53     private func merge(
54         nums: inout [Int],
55         helper: inout [Int],
56         leftIndex: Int,
57         rightIndex: Int,
58         midIndex: Int
59     ) {
60         for i in leftIndex...rightIndex {
61             helper[i] = nums[i]
62         }
63         
64         var currentIndex = leftIndex
65         var left = leftIndex
66         var right = midIndex + 1
67         
68         while(left <= midIndex && right <= rightIndex) {
69             if helper[left] <= helper[right] {
70                 nums[currentIndex] = helper[left]
71                 left += 1
72             } else {
73                 nums[currentIndex] = helper[right]
74                 right += 1
75             }
76             currentIndex += 1
77         }
78         
79         while left <= midIndex {
80             nums[currentIndex] = helper[left]
81             currentIndex += 1
82             left += 1
83         }
84     }
85 }

Runtime: 172 ms
Memory Usage: 23.6 MB
 1 class Solution {
 2     func sortArray(_ nums: [Int]) -> [Int] {
 3         // counting sort
 4         var result:[Int] = [Int](repeating:0,count:nums.count)
 5         var maxNum:Int = Int.min
 6         var minNum:Int = Int.max
 7         for num in nums
 8         {
 9             maxNum = max(num, maxNum)
10             minNum = min(num, minNum)
11         }
12         var count:[Int] = [Int](repeating:0,count:maxNum - minNum + 1)        
13         for i in 0..<nums.count
14         {
15             count[nums[i] - minNum] += 1
16         }        
17         for i in 1..<(maxNum - minNum + 1)
18         {
19             count[i] += count[i - 1]
20         }        
21         for i in stride(from:nums.count - 1,through:0,by:-1)
22         {
23             result[count[nums[i] - minNum] - 1] = nums[i]
24             count[nums[i] - minNum] -= 1
25         }
26         return result
27     }
28 }

184ms

 1 class Solution {
 2     func sortArray(_ nums: [Int]) -> [Int] {
 3         var results = nums.map { $0 + 500000 }
 4         
 5         var last = 1
 6         var base = 10
 7         
 8         while base < 10000000 {
 9             var buckets = Array(repeating: [Int](), count: 10)
10             
11             for num in results {
12                 let remainder = num % base / last
13                 buckets[remainder].append(num)
14             }
15             
16             results = buckets.flatMap { $0 }
17             base *= 10
18             last *= 10
19         }
20         
21         return results.map { $0 - 500000 }
22     }
23 }

212ms

 1 class Solution {
 2     func sortArray(_ nums: [Int]) -> [Int] {
 3         var nums = nums
 4         quicksortLomuto(&nums, low: 0, high: nums.count-1)
 5         return nums
 6     }
 7     
 8     func quicksortLomuto<T: Comparable>(_ a: inout [T], low: Int, high: Int) {
 9         if low < high {
10             let p = partitionLomuto(&a, low: low, high: high)
11             quicksortLomuto(&a, low: low, high: p - 1)
12             quicksortLomuto(&a, low: p + 1, high: high)
13         }
14     }
15 
16     
17     func partitionLomuto<T: Comparable>(_ a: inout [T], low: Int, high: Int) -> Int {
18         let pivot = a[high]
19 
20         var i = low
21         for j in low..<high {
22             if a[j] <= pivot {
23                 (a[i], a[j]) = (a[j], a[i])
24                 i += 1
25             }
26         }
27 
28         (a[i], a[high]) = (a[high], a[i])
29         return i
30     }
31 }

 1 class Solution {
 2     func sortArray(_ nums: [Int]) -> [Int] {
 3         return nums.quickSorted()
 4     }
 5 }
 6 
 7 extension Array where Element == Int {
 8     func quickSorted() -> [Element] {
 9         guard !isEmpty else {
10             return []
11         }
12         var elements = self
13         quickSort(&elements, left: 0, right: self.count - 1)
14         return elements
15     }
16 
17     private func median(_ x: Int, y: Int, z: Int) -> Int {
18         if x < y {
19             if y < z {
20                 return y
21             } else if z < x {
22                 return x
23             } else {
24                 return z
25             }
26         } else {
27             if z < y {
28                 return y
29             } else if x < z {
30                 return x
31             } else {
32                 return z
33             }
34         }
35     }
36 
37     private func quickSort(_ elements: inout [Int], left: Int, right: Int) {
38         if (left < right) {
39             var i = left
40             var j = right
41             let pivot = median(elements[i], y: elements[i + (j - i) / 2], z: elements[j])
42             while true {
43                 while elements[i] < pivot {
44                     i += 1
45                 }
46                 while pivot < elements[j] {
47                     j -= 1
48                 }
49                 guard i < j else {
50                     break
51                 }
52                 let tmp = elements[i]
53                 elements[i] = elements[j]
54                 elements[j] = tmp
55                 i += 1
56                 j -= 1
57             }
58             quickSort(&elements, left: left, right: i - 1)
59             quickSort(&elements, left: j + 1, right: right)
60         }
61     }
62 }

 1 class Solution {
 2     func sortArray(_ nums: [Int]) -> [Int] {
 3         var nums = nums
 4         quickSort(&nums, 0, nums.count - 1)
 5         return nums
 6     }
 7     
 8     func quickSort(_ nums: inout [Int], _ left: Int, _ right: Int) {
 9         if left < right {
10             let pivot = partition(&nums, left, right)
11             quickSort(&nums, left, pivot - 1)
12             quickSort(&nums, pivot + 1, right)
13         }
14     }
15 
16     func partition(_ nums: inout [Int], _ left: Int, _ right: Int) -> Int {
17         let pivot = nums[right]
18         var i = left - 1
19         for j in left..<right {
20             if nums[j] < pivot {
21                 i += 1
22                 nums.swapAt(i, j)
23             }
24         }
25         nums.swapAt(i + 1, right)
26         return i + 1
27     }
28 }

 

 

posted @ 2019-05-18 16:59  为敢技术  阅读(379)  评论(0编辑  收藏  举报