Sorting algorithms are often used, such as: bubbling, selection, quick sorting, binary insertion sorting, and so on. The front two does not say, paste out their own quick sort and binary insert sort of swift and Java code implementation, like the words control+c to use.
Swift enables quick sorting and binary insertion sorting algorithms:
(If you want to run, just copy the code and replace the Viewdidload method in the controller)
Override Func Viewdidload () {
Super.viewdidload ()
Additional setup after loading the view, typically from a nib.
var array:array<int> = [2,1,3,5,4,6,8,7,9,10]
QuickSort (&array, left:0, Right:9)
Binarysort (&array)
For i in 0 ... array.count-1{
Print (Array[i])
}
}
Quick Sort
Func quickSort (inout array:array<int>,left:int,right:int) {
If left > right {
Return
}
var base,temp,i,j:int
i = Left
j = Right
Base = Array[left]
While I < J {
While Array[j] >= base && i < J {
J-= 1
}
While Array[i] <= base && i < J {
i + = 1
}
If I < J {
temp = Array[i]
Array[i] = Array[j]
ARRAY[J] = Temp
}
}
Array[left] = Array[i]
Array[i] = base
QuickSort (&array, Left:left, right:i-1)
QuickSort (&array, left:i + 1, right:right)
}
Two-point insertion method ordering
Func binarysort (inout array:array<int>) {
var low,mid,high,base:int
For I in 1 ... array.count-1{
Low = 0
High = I-1
Base = Array[i]
While low <= high {
Mid = (low + high)/2
If Array[mid] >= base{
Low = mid + 1
}else if Array[mid] <= base{
High = Mid-1
}
}
for var j = i-1; J >= High + 1; J-= 1{
Array[j + 1] = Array[j]
}
Array[high + 1] = base;
}
}
Java enables quick ordering:
Directly paste out the entire class, after all, not long, create a new file to copy all the code into it can be run.
public class sort{
public static void QuickSort (int[] Array,int left,int right) {
if (Left > right) {
Return
}
int base,temp,i,j;
i = left;
j = right;
base = Array[left];
while (I < j) {
while (Array[j] <= base && i < J) {
J--;
}
while (Array[i] >= base && i < J) {
i + +;
}
if (I < j) {
temp = Array[i];
Array[i] = Array[j];
ARRAY[J] = temp;
}
}
Array[left] = Array[i];
Array[i] = base;
QuickSort (array,left,i-1);
QuickSort (array,i + 1,right);
}
public static void Main (string[] args) {
int[] array = new int[]{2,1,4,3,6,5,8,7,9,10};
QuickSort (array,0,9);
for (int i = 0; i <; i + +) {
System.out.println (Array[i]);
}
}
}
Java implementation of the binary insertion method of ordering:
public class binarysort{
public static void Main (string[] args) {
int[] array = new int[]{2,3,1,4,6,5,8,7,9,10};
Binarysort (array);
for (int numebr:array) {
System.out.println (NUMEBR);
}
}
public static void Binarysort (int[] array) {
int low,mid,high,base;
for (int i = 1; i < Array.Length; i++) {
Low = 0;
High = i-1;
base = Array[i];
while (low <= high) {
Mid = (low + high)/2;
if (Array[mid] > base) {
Low = mid + 1;
}else if (Array[mid] < base) {
High = mid-1;
}
}
for (int j = i-1, J >= High + 1; J-= 1) {
Array[j + 1] = Array[j];
}
Array[high + 1] = base;
}
}
}
The above is the implementation of the fast sorting algorithm and the binary insertion algorithm using Swift and Java, and the test cases. The shortcomings, please correct me.
Quick Sort/Two minute insert sort of swift and Java implementation