选择排序
public class SelectSort {
public static void main(String[] args){
int[] a = {5,3,6,4,8,9,2,5};
for(int i=0;i<a.length-1;i++){
int minpos = i;
for(int j=i+1;j<a.length;j++){
minpos = a[j]<a[minpos]?j:minpos;
}
swap(a,minpos,i);
}
print(a);
}
public static void swap(int[] arr,int i,int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void print(int[] arr){
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
}
}
冒泡排序
public class BubbleSort {
public static void main(String[] args) {
int[] a = {9, 5, 8, 4, 3, 6, 1, 2};
sort(a);
print(a);
}
public static void sort(int[] arr) {
for (int i = arr.length - 1; i >= 0; i--) {
for (int j = 0; j < i; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr, j, j + 1);
}
}
}
}
public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void print(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
插入排序
public class InsertSort {
public static void main(String[] args){
int[] a = {9,5,8,4,3,6,1,2};
sort(a);
print(a);
}
public static void sort(int[] arr){
for(int i=1;i<arr.length;i++){
for (int j=i;j>0;j--){
if(arr[j]<arr[j-1]){
swap(arr,j,j-1);
}
}
}
}
public static void swap(int[] arr,int i,int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void print(int[] arr){
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
System.out.println();
}
}
希儿排序
public class ShellSort {
public static void main(String[] args){
int[] a = {9,5,8,4,3,6,1,2};
sort(a);
print(a);
}
public static void sort(int[] arr){
for(int gap = arr.length/2;gap>0;gap/=2) {
for (int i = gap; i < arr.length; i++) {
for (int j = i; j > gap-1; j-=gap) {
if (arr[j] < arr[j - gap]) {
swap(arr, j, j - gap);
}
}
}
}
}
public static void swap(int[] arr,int i,int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void print(int[] arr){
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
System.out.println();
}
}
归并排序
public class MergeSort {
public static void main(String[] args){
int[] a = {9,5,8,4,3,6,1,2};
sort(a,0,a.length-1);
print(a);
}
public static void merge(int[] arr,int l1,int r1,int l2,int r2){
int i=l1,j=l2,index=0;
int[] temp = new int[r2-l1+1];
while(i<=r1&&j<=r2){
temp[index++] = arr[i]<=arr[j] ? arr[i++]:arr[j++];
}
while (i<=r1) temp[index++] = arr[i++];
while (j<=r2) temp[index++] = arr[j++];
for(int k=0;k<index;k++){
arr[l1+k] = temp[k];
}
}
public static void sort(int[] arr,int left,int right){
if (left == right||arr.length<2) {
return;
}
if(left<right){
int mid = left+(right-left)/2;
sort(arr,left,mid);
sort(arr,mid+1,right);
merge(arr,left,mid,mid+1,right);
}
}
public static void swap(int[] arr,int i,int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void print(int[] arr){
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
System.out.println();
}
}
快速排序
public class QuickSort {
public static void main(String[] args){
int[] a = {9,5,8,4,3,6,1,2,12,45,0,9};
sort(a,0,a.length-1);
print(a);
}
public static int part(int[] arr, int left, int right){
//随机化处理
int pos = left + (int) ( Math.random() * (right-left+1) );
swap(arr,pos,left);
//保证n*logn
int temp = arr[left];
while(left<right){
while(left<right&&arr[right]>=temp) right--;
arr[left] = arr[right];
while(left<right&&arr[left]<temp) left++;
arr[right] = arr[left];
}
arr[left] = temp;
return left;
}
public static void sort(int[] arr,int left,int right){
if(left<right){
int mid = part(arr,left,right);
sort(arr,left,mid-1);
sort(arr,mid+1,right);
}
}
public static void swap(int[] arr,int i,int j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void print(int[] arr){
for(int i=0;i<arr.length;i++){
System.out.print(arr[i]+" ");
}
System.out.println();
}
}
堆排序
public class HeapSort {
public static void main(String[] args) {
int[] a = {9, 5, 8, 4, 3, 6, 1, 2 , 13 , 15 , 14};
heapSort(a);
print(a);
}
public static void heapSort(int[] a) {
if (a==null||a.length<2) {
return;
}
for (int i=0;i<a.length;i++) {
heapInsert(a, i);
}
int heapSize = a.length;
swap(a,0,--heapSize);
while (heapSize>0) {
heapify(a,0,heapSize);
swap(a,0,--heapSize);
}
}
public static void heapInsert(int[] a, int index) {
while (a[index]>a[(index-1)/2]){
swap(a,index, (index-1)/2);
index = (index-1)/2;
}
}
public static void heapify(int[] a,int index,int heapSize) {
int left = index * 2 + 1;
while (left<heapSize) {
int largest = left + 1 < heapSize && a[left+1] > a[left]
? left+1:left;
largest = a[largest] > a[index]? largest:index;
if (largest == index) {
break;
}
swap(a,largest,index);
index = largest;
left = index * 2 + 1;
}
}
public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void print(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}