//Playground-noun:a Place where people can playImport Cocoavar nums=int[] () for_inch 1... -{nums.append (random ()))}nums////Bubble Sort/*var count = 0;for (var i = 0; i < nums.count-1; i++) {for (var j = 0; J < nums.count-i-1;j++) {count++; if (Nums[j] > nums [j+1]) {Let temp = nums[j]; NUMS[J] = nums[j+1]; NUMS[J+1] = temp; }}}count*/////Bubble Sort Optimized edition/*var count = 0;var flag = True;for (var i = 0; i < nums.count-1 && flag; i++) {//outer shell = false; for (var j = 0; J < nums.count-i-1; j + +) {//inner loop count++; if (Nums[j] > nums[j + 1]) {//compare size, large put back, small put front var temp = nums[j]; NUMS[J] = nums[j + 1]; Nums[j + 1] = temp; Flag = true; }}}count*//////Select Sort/*for (var i = 0; i < Nums.count; i++) {//outer sweep face control var min = i;//is used to record the subscript with the lowest value, the default assumes that the first element of the array is minimal. for (var j = i; J < Nums.count; J + +) {if (Nums[j] < Nums[min]) {//To find the subscript min of the element in the min subscript = j;// Hold min always the smallest element of the small mark} if I! = min {//a trip to determine whether the minimum value is found, if there is a switch, the minimum value is moved to the front end of the scan data var temp = n Ums[i]; Nums[i] = Nums[min]; Nums[min] = temp; }}nums*///Insert Sort/*for (var i = 1; i < Nums.count; i++) {//outer layer scan of unordered list if (Nums[i] < nums[i-1]) {var temp = nums[i];// Save the value of the point, and then insert the value in the appropriate location var j = i-1; for (; J >= 0 && temp < nums[j]; j--) {//Move backward tips here do not be nums[i] < nums[j] to compare,//Because nums[i] this value Is overwritten after a shift, so why use temp to save this value nums[j + 1] = nums[j];//Array Backward} nums[j + 1] = temp; }}nums*///Hill Sort/*var increment = nums.count;//increment while (Increment > 1) {increment = INCREMENT/2;//increment calculation//Once basic same as insert sort, just insert sort directly we compare When the increment is 1,shell, the sort setting has its own increment for (var i = increment; i<nums.count;i++) {if (Nums[i] < nums[i-increment]) { var temp = nums[i]; var j = i-increment; for (; J >= 0 && temp < nums[j]; J-= increment) {Nums[j+increment] = nums[j]; } Nums[j+increment] = temp}}}nums*//*//Heap sort func heapadjust (nums:int[],node:int,length:int) {if (node*2+1) <= length- 1) {//Ensure that the node is a non-leaf node, because the leaf node is meaningless. var child = node * 2 + 1;//byte point coordinates, mainly after the exchange needs to judge the child for the node and adjust Dagen if (Child + 1 & lt;= length-1) {//If there is a right subtree if (Nums[child + 1] > Nums[child]) {child++;//After judging if the right subtree is greater than the left subtree, child+ +, that is, wait for the left subtree node} if (Nums[node] < Nums[child]) {//large subtree vs. node//Swap var temp = Nums[node]; Nums[node] = Nums[child]; Nums[child] = temp; Re-refactor Heapadjust (nums, child, length); }}}var n = nums.count;//step1: The sequence is constructed into a large heap for (var i = (n-1)/2; I >= 0; i--) {//The large heap construction of each node and its child nodes Heapadju St (Nums, I, n);} NUMS//STEP2: Traverse the maximum element to move to the end for (var x = (n-1); x > 0; x--) {var temp = nums[x]; NUMS[X] = nums[0]; Nums[0] = temp; Step3: Refactoring heapadjust (nums, 0, x);} Nums *///Merge Sort/*Func merging (nums:int[], Head:int, Mid:int, tail:int) {var Mius = tail-head+1 var temp:int[] = Int [] ()///tail-head+1//Apply extra space for _ in 1...mius {temp.append (0)} var = head; var Lowtow = head; var lowtowfine = head; var = tail; var j = mid + 1; var p = 0; Mius//Two sub-sequences are non-empty while (head <= mid && J <= tail) {if (Nums[head] > Nums[j]) {TE mp[p++] = nums[j++]; } else {temp[p++] = nums[lowtow++]; }}//The first subsequence is not empty, copy the remaining elements into temp while (head <= mid) {//temp[p++] = nums[lowtowfine++]; }//Second subsequence is non-null, copy the remaining elements into temp while (J <= tail) {//temp[p++] = nums[j++]; }//The brush in the cache into the merge sequence for (var q = 0, t = low; t <= High; q++, t++) {nums[t]=temp[q];//Merge results are copied back to R[low after completion. High]}//merge}var length = Nums.count;for (var n = 1; n < nums.count; n *= 2) {//Do Logn trip merge var I:int; for (i = 0; i +2 * n-1 <= length-1; i = i + 2 * N) {Merging (nums, I, i + n-1, i + 2 * n-1);//merge two adjacent sub-files of length =} if (i + n-1 < nums. COUNT-1)//There are two sub-files, of which the latter length is less than length {merging (nums, I, i + n-1, nums.count-1);//Merge the last two sub-files}}nums*//*Quick sort func partition (nums:int[], Lower:int, higher:int), Int {var key = Nums[lower]; var numstwo:int[] = nums//var temp:int[]; var high:int = higher var low:int = lower while low < high {when (Low < high && Nu Mstwo[high] >= key) {high--} if (Low < high) {var temp = Numst Wo[low]; Numstwo[low] = Numstwo[high]; Numstwo[high] = temp; } while (Low < high && Numstwo[low] <= key) {low++; } if (Low < high) {var temp = Numstwo[low]; Numstwo[low] = Numstwo[high]; Numstwo[high] = temp; }} return low;} Func QuickSort (nums:int[], Low:int, high:int) {var mid:int; if (Low < high) {mid = partition (Nums, Low, high); QuickSort (Nums, Low, mid-1); QuickSort (Nums, mid + 1, high); }}quicksort (nums,0,0)*/
Basic integration is usually the basic algorithm:
Approximate integration:
Bubble sort, select sort, insert sort
Quick Sort by merge sort, hill sort, heap sort
Each of these sorts has an optimal ordering method, which requires more practice, pondering