Summary of common sorting algorithms

Bubble Sort:

Introduction to Bubble Sort:
Bubble Sort is a relatively simple sorting algorithm in the field of computer science. It repeatedly walks through the sequence to be sorted, comparing two elements at a time, and swapping them if they are in the wrong order. The work of visiting the sequence is repeated until there are no more elements to be exchanged, that is, the sequence has been sorted. The name of this algorithm comes from the fact that larger elements will slowly "float" to the top of the sequence by swapping, just like bubbles, hence the name bubble sort.

Principle of bubble sort algorithm:
1. Compare two adjacent elements. If the first element is larger than the second element, swap the two elements.
2. Do the same for each pair of adjacent elements, from the first pair at the beginning to the last pair at the end. After one pass of this strategy, the last element will be the largest number.
3. Exclude the last element and repeat the above two steps for all the remaining elements.
4. Continue to repeat the above steps for fewer and fewer elements until there are no pairs of elements to compare.

The best time complexity of bubble sort is O(N), and the worst time complexity is O(N²), so the total average time complexity of bubble sort is O(N²).

/**
 * Bubble sort algorithm implementation
 * @param destArray The unordered array to be sorted, such as: 65 27 59 64 58
 */
public static void bubbleSort(int[] destArray)
{
	for(int i = 0;i < destArray.length;i++)
	{
	   for(int j = i;j < destArray.length;j++)
	   {
	      if(destArray[j] < destArray[i])
	      {
		     int temp = destArray[i];
		     destArray[i] = destArray[j];
		     destArray[j] = temp;
	      }
	   }
	}
}

Quick Sort:
Introduction to Quick Sort:
Quick Sort is an improvement on Bubble Sort. Its basic idea is: divide the data to be sorted into two independent parts by one sorting, all the data in one part is smaller than all the data in the other part, and then use this method to quickly perform the two parts of the data respectively. Sorting, the entire sorting process can be performed recursively, so that the entire data becomes an ordered sequence.

Principle of quick sort algorithm:
Assuming that the array to be sorted is A[0]...A[N-1], first randomly select a data (usually the first number of the array) as the key data, and then put all the data smaller than it All numbers are placed in front of it, and all numbers larger than it are placed behind it. This process is called a quick sort. It is worth noting that quicksort is not a stable sorting algorithm, that is, the relative positions of multiple identical values ​​may change at the end of the algorithm.
The algorithm of a quick sort is:
1. Set two variables i and j, when the sorting starts: i=0, j=N-1;
2. Use the first array element as the key data and assign it to the key, that is key=A[0];
3. Search forward from j, that is, search forward from the back (j--), find the first value A[j] less than the key, and compare A[j] and A[ Swap i];
4. Search backwards from i, that is, search backwards from the front (i++), find the first A[i] greater than the key, and swap A[i] and A[j];
5. Repeat steps 3 and 4 until i=j; (In steps 3 and 4, no qualified value is found, that is, when A[j] in 3 is not less than key, and A[i] in 4 is not greater than key Change the values ​​of j and i so that j=j-1, i=i+1, until found. Find a value that meets the conditions, and the position of the i and j pointers will remain unchanged when exchanging. In addition, i==j this The process must be exactly when i+ or j- completes, at which point the loop ends).

The best time complexity of quicksort is O(NlogN), and the worst time complexity is O(N²), so the total average time complexity of quicksort is O(NlogN).

/**
 * Quick sort algorithm implementation
 * @param destArray The unordered array to be sorted, such as: 65 27 59 64 58
 * @param low the low index of the array
 * @param high the high index of the array
 */
public static void quickSort(int[] destArray, int low, int high)
{
	if(low >= high)
	{
		return;
	}
	
	int first = low;
	int last = high;
	int key = destArray[first];
	
	while(first < last)
	{
		while(first < last && destArray[last] >= key)
		{
			last--;
		}
		destArray[first] = destArray[last];//Move smaller than the first to the low end
		
		while(first < last && destArray[first] <= key)
		{
			first++;
		}
		destArray[last] = destArray[first];
	}
	
	destArray[first] = key;
	quickSort(destArray, low, first - 1);
	quickSort(destArray, first + 1, high);
}

Insertion Sort:
Introduction to Insertion Sort:
Insert Sort (Insert Sort) Its basic idea is: input an element, check each element in the array list, and insert it into an already sorted sequence. Still ordered, the array is sorted when the last element is put into place.
The principle of insertion sort algorithm:
input a number, insert it into an array whose elements have been arranged in ascending order, and make the elements in the array still in ascending order after insertion. Idea: Compare the number to be inserted with the numbers in the array one by one. When the first element i that is larger than the inserted number is found, the insertion position is before the element. Then, starting from the last element of the array and ending at that element, move backward one cell one by one. Finally, assign the number of insertions to the element a[i]. If the inserted number is less than the value of all elements, insert the first position.
The best time complexity of insertion sort is O(N) and the worst time complexity is O(N²), so the total average time complexity of bubble sort is O(N²).

/**
 * Insertion sorting algorithm implementation (from front to back)
 * @param destArray The unordered array to be sorted, such as: 65 27 59 64 58
 */
public static void insertSort(int[] destArray)
{
	//Insert from the second data, so here starts from i=1
	for(int i = 1;i < destArray.length;i++)
	{
		int j = 0;
		//find where to insert
		while(j < i && destArray[j] <= destArray[i])
			j++;
		
		//Before the i position, if there is a number larger than destArray[i], move and insert
		if(j < i)
		{
			int k = i;
			int temp = destArray[i];
			
			//move position
			while(k > j)
			{
				destArray[k] = destArray[k-1];
				k--;
			}
			destArray[k] = temp;//Insert
		}
	}
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326376171&siteId=291194637