package _01_Sorted;
import org.junit.Test;
import java.util.*;
public class all {
public void select(int[] arr){
for (int i = 0; i < arr.length; i++) {
int min = arr[i];
int minIndex = i;
for (int j = i+1; j <arr.length ; j++) {
if(min>arr[j]){
min = arr[j];
minIndex = j;
}
}
if(minIndex!=i){
arr[minIndex] = arr[i];
arr[i] = min;
}
}
}
public void insert(int[] arr){
for (int i = 0; i < arr.length; i++) {
int insertVal = arr[i];
int j;
for (j = i; j >0 && insertVal<arr[j-1] ; j++) {
arr[j] = arr[j-1];
}
arr[j] = insertVal;
}
}
public void shell(int[] arr){
for (int step = arr.length/2; step >=1 ; step/=2) {
for (int i = step; i <arr.length ; i++) {
int insertVal = arr[i];
int j;
for (j = i; j >=step && insertVal < arr[j-step] ; j-=step) {
arr[j] = arr[j-step];
}
arr[j] = insertVal;
}
}
}
public void bubble(int[] arr){
boolean flag = false;
for (int i = 0; i < arr.length-2; i++) {
for (int j = 0; j < arr.length-i-1; j++) {
if(arr[j]>arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
flag = true;
}
}
if(!flag){
break;
}else{
flag = false;
}
}
}
public void quick(int[] arr,int left,int right){
if(left<right){
int base = left + (int)(Math.random()*(right-left));
swap(arr,base,left);
int i = left,j = right;
while(i<j){
while(i<j && arr[j]>arr[left]) j--;
while(i<j && arr[i]>arr[left]) i++;
swap(arr,i,j);
}
swap(arr,left,j);
quick(arr,left,j-1);
quick(arr,j+1,right);
}
}
public void swap(int[]arr, int i,int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
@Test
public void test(){
int[] arr = {2,5,1,8,0,3,7};
quick(arr,0,arr.length-1);
System.out.println(Arrays.toString(arr));
}
}
|