Several common algorithms

Bubble algorithm

for(int i=0;i<a.length;i++) {
			for(int j=i;j<a.length;j++) {
				if(a[j]>a[j+1]) {
					int temp=a[j];
					a[j]=a[j+1];
					a[j+1]=temp;
				}
			}
		}

 selection sort

for(int i=0;i<a.length;i++) {
			for(int j=i;j<a.length;j++) {
				if(a[i]>a[j]) {
					int temp=a[i];
					a[i]=a[j];
					a[j]=temp;
				}
			}
		}

 Insertion sort, poker pattern

for (int i = 0; i < a.length; i++) {
			for (int j = i; j > 0 && a[j] < a[j - 1]; j--) {
				int temp = a[j - 1];
				a[j - 1] = a[j];
				a[j] = temp;

			}
		}

 Hill sort

int h = 1;
		while (h < a.length / 3)
			h = 3 * h + 1;
		while (h >= 1) {
			for (int i = 0; i < a.length; i++) {
				for (int j = i; j > 0 && a[j] < a[j - 1]; j--) {
					int temp = a[j - 1];
					a[j - 1] = a[j];
					a[j] = temp;

				}
			}
			h=h/3;
		}

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326681844&siteId=291194637