JAVA common sorting algorithms (bubble, select, insert, fast, heap sort)

Copyright: arbitrary, seeding https://blog.csdn.net/qq_32662595/article/details/84787239!

[GIT Source Address] https://gitee.com/UniQue006/algorithm.git

Bubble Sort

Sample Code ->

     package com.hounds;    
        public class Bubbling {
        	public static void main(String[] args) {
        		int[] arrays = new int[] { 22, 33, 44, 11, 23, 76, 88, 90, 6 };
        		new Bubbling().sort(arrays);
        		for (int i = 0; i < arrays.length; i++) {
        			System.out.print(arrays[i]+" ");
        		}
        	}
        	public void sort(int[] nums) {
        		int len = nums.length;
        		for (int i = 0; i < len; i++) {
        			for (int j = 0; j < len - i - 1; j++) {
        				if (nums[j] > nums[j + 1]) {
        					int temp = nums[j];
        					nums[j] = nums[j + 1];
        					nums[j + 1] = temp;
        				}
        			}
        		}
        	}
        }

Selection sort
Sample Code ->

public class Selection {
	public static void main(String[] args) {
		int[] arrays = new int[] { 22, 33, 44, 11, 23, 76, 88, 90, 6 };
		new Bubbling().sort(arrays);
		for (int i = 0; i < arrays.length; i++) {
			System.out.print(arrays[i]+" ");
		}
	}
	public void sort(int[] nums) {
		int len = nums.length;		
		for(int i=0; i<len; i++){
			for(int j=i+1; j<len; j++){
				if(nums[i] > nums[j]){
					int temp = nums[j];
					nums[j] = nums[i];
					nums[i] = temp;
				}
			}
		}
	}
}

Insertion sort
Sample Code ->

public class InsertSort {
	public static void main(String[] args) {
		int[] arrays = new int[] { 22, 33, 44, 11, 23, 76, 88, 90, 6 };
		new Bubbling().sort(arrays);
		for (int i = 0; i < arrays.length; i++) {
			System.out.print(arrays[i] + " ");
		}
	}
	public void sort(int[] nums) {
		int len = nums.length;
		for (int i = 1; i < len; i++) {
			int temp = nums[i];
			int j = i - 1;
			for (; j >= 0 && nums[j] > temp; j--) {
				nums[j + 1] = nums[j];
			}
			nums[j + 1] = temp;
		}
	}
}

Quicksort
sample code ->

public class FasterSort {
	public static void main(String[] args) {
		int[] arrays = new int[] { 22, 33, 44, 11, 23, 76, 88, 90, 6 };
		new Bubbling().sort(arrays);
		for (int i = 0; i < arrays.length; i++) {
			System.out.print(arrays[i] + " ");
		}
	}
	public void sort(int[] nums, int low, int high) {
		int i = low;
		int j = high;
		int base = nums[low];
		while (i < j) {
			while (i < j && nums[j] > base)
				j--;
			if (i < j) {
				int temp = nums[j];
				nums[j] = nums[i];
				nums[i] = temp;
				i++;
			}
			while (i < j && nums[i] <= base)
				i++;
			if (i < j) {
				int temp = nums[j];
				nums[j] = nums[i];
				nums[i] = temp;
				j--;
			}
		}
		if (i > low)
			sort(nums, low, i - 1);
		if (j < high)
			sort(nums, j + 1, high);
	}
}

Heap sort
Sample Code ->

public class HeapSort {
	public static void main(String[] args) {
		int[] arrays = new int[] { 22, 33, 44, 11, 23, 76, 88, 90, 6 };
		new Bubbling().sort(arrays);
		for (int i = 0; i < arrays.length; i++) {
			System.out.print(arrays[i] + " ");
		}
	}

	public void sort(int[] nums) {
		createHeap(nums);
		int len = nums.length;
		for (int i = len - 1; i > 0; i--) {
			int temp = nums[i];
			nums[i] = nums[0];
			nums[0] = temp;
			foundHeap(nums, 0, i);
		}
	}

	public void createHeap(int[] nums) {
		int len = nums.length;
		for (int i = nums.length / 2 - 1; i >= 0; i--) {
			foundHeap(nums, i, len);
		}
	}

	public void foundHeap(int[] nums, int i, int len) {
		int left = i * 2 + 1;
		int right = i * 2 + 2;
		int max = 0;
		if (left < len && nums[left] > nums[i]) {
			max = left;
		} else {
			max = i;
		}
		if (right < len && nums[max] < nums[right]) {
			max = right;
		}
		if (max != i) {
			int temp = nums[max];
			nums[max] = nums[i];
			nums[i] = temp;
			foundHeap(nums, max, len);
		}
	}
}

Guess you like

Origin blog.csdn.net/qq_32662595/article/details/84787239