# Data structure and algorithm - compilation of sorting algorithm assistant tool and three basic sorting algorithms (Java implementation)

1, Sorting algorithm auxiliary tool class:

1. Randomly generate an array of integers
2. Generate a nearly ordered array of integers
3. Judge whether an array is in order, return true in order, return false in order
4. Copy an integer array
5. Swap the positions of two indexes in an array
6. Print array
public class SortTestUtils {

/**
* 	Randomly generate an array of integers
* @param size		Array size
* @param rangeL	Array left range (inclusive)
* @param rangeR	Range to the right of the array (inclusive)
* @return
*/
public static int[] generateRandomArray(int size,int rangeL,int rangeR) {
int[] arr = new int[size];
for(int i = 0;i < size;i++) {
arr[i] = (int) (Math.random() * (rangeR-rangeL+1)) + rangeL;
}
return arr;
}

/**
* Generate a nearly ordered array of integers
* @param size		Array size
* @param swapTimes	Number of times to scramble an ordered array
* @return
*/
public static int[] generateAlmostOrderlyArray(int size,int swapTimes) {
int[] arr = new int[size];
for(int i = 0;i<size;i++) {
arr[i] = i;
}
for(int i = 0;i < swapTimes;i++) {
int index1 = (int) Math.random()*size;
int index2 = (int) Math.random()*size;
swap(arr,index1,index2);
}
return arr;
}

/**
* Judge whether an array is in order, return true in order, return false in order
* @param arr	Shaping array
* @return
*/
public static boolean isOrderly(int[] arr) {
for(int i=0;i < arr.length-1;i++) {
if(arr[i] > arr[i+1]) {
return false;
}
}
return true;
}

/**
* Copy an integer array
* @param arr	Shaping array
* @return
*/
public static int[] copyIntArray(int[] arr) {
int[] copyArr = new int[arr.length];
for(int i = 0;i < arr.length; i++) {
copyArr[i] = arr[i];
}
return copyArr;
}

/**
* Swap the positions of two indexes in an array
* @param arr	Shaping array
* @param i		First index
* @param j		Second index
*/
public static void swap(int[] arr, int i, int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

/**
* 	Print array
* @param arr	Shaping array
*/
public static void printArray(int[] arr) {
for (int i : arr) {
System.out.print(i+"\t");
}
System.out.println();
}

}

2, Three basic sorting algorithms:

1. Bubble sort
2. Selection sort
3. Insertion sort
/**
* Bubble sort
* @param arr	Shaping array
*/
public static void bubbleSort(int[] arr) {
for(int i = 0;i < arr.length-1;i++) {
for(int j = 0;j < arr.length-1;j++) {
if(arr[j+1] < arr[j]) {
SortTestUtils.swap(arr, j+1, j);
}
}
}
}

/**
* Selection sort
* 		-Select a minimum number, insert the leftmost
* @param arr	Shaping array
*/
public static void selectSort(int[] arr) {
for(int i = 0;i < arr.length-1;i++) {
int min = i;
for(int j = i+1;j < arr.length;j++) {
if(arr[j] < arr[min]) {
min = j;
}
}
SortTestUtils.swap(arr, i, min);
}
}

/**
* Insertion sort
* 		-Randomly select a number and insert it into the appropriate position
* @param arr	Shaping array
*/
public static void insertSort(int[] arr) {
for(int i = 1; i < arr.length; i++) {
int insertValue = arr[i];
int j = 0;
for(j = i;j > 0 && arr[j-1] > insertValue; j--) {
arr[j] = arr[j-1];
}
arr[j] = insertValue;
}
}
Published 4 original articles, won praise 0, visited 32

Posted by ternto333 on Sat, 11 Jan 2020 09:28:21 -0800