Home> headlines> body text

Detailed explanation of sorting algorithm

小云云
Release: 2017-12-04 10:58:55
Original
1997 people have browsed it

The so-called sorting is the operation of arranging a string of records in increasing or decreasing order according to the size of one or some keywords in it. Sorting algorithm is how to arrange records as required. Sorting algorithms have received considerable attention in many fields, especially in the processing of large amounts of data. An excellent algorithm can save a lot of resources.

Simple Insertion Sort insertion sort

/** * 将位置p上的元素向左移动,直到它在前p+1个元素中的正确位置被找到的地方 * @param a an array of Comparable items */public static > void insertionSort(AnyType[] a) { int j; for (int p = 1; p < a.length; p++) { AnyType tmp = a[p]; for (j = p; j > 0 && tmp.compareTo(a[j-1]) < 0; j--) { a[j] = a[j-1]; } a[j] = tmp; } System.out.println(Arrays.toString(a));}
Copy after login

Shell Sort Hill sort

/** * @param a an array of Comparable items */public static > void shellSort(AnyType[] a) { int j; for (int gap = a.length / 2; gap > 0; gap /= 2) { for (int i = gap; i < a.length; i++) { AnyType tmp = a[i]; for (j = i; j >= gap && tmp.compareTo(a[j - gap]) < 0; j -= gap) { a[j] = a[j - gap]; } a[j] = tmp; } } System.out.println(Arrays.toString(a)); }
Copy after login

Binary Sort binary sort

/** * @param a an array of Comparable items */public static > void binarySort(AnyType[] a) { Integer i,j; Integer low,high,mid; AnyType temp; for(i=1;ihigh;j--) a[j+1]=a[j]; a[high+1]=temp; } System.out.println(Arrays.toString(a)); }
Copy after login

Bubble Sort Bubble Sort

/** * @param a an array of Comparable items */public static > void bubbleSort(AnyType[] a) { Integer i,j; AnyType temp; for(i=1;i 0) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; //将"气泡"下沉到当前比较的最后一位 } } } System.out.println(Arrays.toString(a)); }
Copy after login

Selection Sort Selection Sort

/** * @param a an array of Comparable items */public static > void selectSort(AnyType[] a) { Integer i,j,min; AnyType temp; for(i=0;i 0) { //用a[i]和后面所有元素逐个比较,找到最小指的下标并记录 temp=a[j]; //下一位小于前一位,则将下一位赋值给temp并继续往右移动比较 min=j; //最小值的下标,赋值给min } } a[min] = a[i]; //将最小值元素的和当前元素交换,使得当前元素为其后面所有元素中最小值 a[i] = temp; } System.out.println(Arrays.toString(a));
Copy after login

The above content is just a few kinds of sorting Algorithm tutorial, I hope it can help everyone.

Related recommendations:

Example analysis of basic commonly used sorting algorithms in JavaScript

Detailed explanation of PHP heap sorting algorithm examples

Detailed explanation of several sorting algorithm examples in php

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!