在牛客网上面把排序算法写了一遍,遇到了很多边界的问题,调试了挺久的,现在把正确代码记录下来,已被不时之需
首先有图有真相
冒泡排序
import java.util.*;
public class BubbleSort {
public int[] bubbleSort(int[] A, int n) {
if(A==null&&A.length<2)
return A;
Boolean flag = true;
for(int i=0;i<A.length&&flag;i++){
for(int j=i+1;j<A.length;j++){
if(A[i]>A[j]){
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
}
return A;
}
}
选择排序
import java.util.*;
public class SelectionSort {
public int[] selectionSort(int[] A, int n) {
if(A==null&&n<2)
return A;
// write code here
int min ;
for(int i=0;i<n;i++){
min = i;
for(int j=i+1;j<n;j++){
if(A[min]>A[j])
min = j;
}
if(min!=i){
int temp = A[i];
A[i] = A[min];
A[min] = temp;
}
}
return A;
}
}
插入排序
import java.util.*;
public class InsertionSort {
public int[] insertionSort(int[] A, int n) {
// write code here
for(int i=1;i<n;i++){
for(int j=i;j>0&&A[j-1]>A[j];j--){
int temp = A[j];
A[j] = A[j-1];
A[j-1] = temp;
}
}
return A;
}
}
快速排序
import java.util.*;
public class QuickSort {
public int[] quickSort(int[] A, int n) {
// write code here
quick(A,0,n);
return A;
}
public void quick(int[] arr,int left,int right){
if(left>=right)
return;
int temp = arr[left];
int i,j;
i = left;
j = right;
while(true){
//从左边开始扫描找到一个比middle大的
while((++i)<right-1 && arr[i]<temp);
//从右边开始扫面找到一个比middle小的
while((--j)>left && arr[j]>temp);
if(i>=j)
break;
int a = arr[i];
arr[i] = arr[j];
arr[j] = a;
}
arr[left] = arr[j];
arr[j] = temp;
quick(arr, left, j);
quick(arr, i, right);
}
}
堆排序
import java.util.*;
public class HeapSort {
public int[] heapSort(int[] A, int n) {
//建立大顶堆
for(int i=n/2;i>=0;i--){
adjustHeap(A,i,n);
}
//第一个元素和最后一个元素交换
for(int i=n-1;i>=0;i--){
int temp = A[0];
A[0] = A[i];
A[i] = temp;
adjustHeap(A,0,i);
}
return A;
// write code here
}
public void adjustHeap(int[] A,int index,int n){
int temp = A[index];
for(int j=2*index+1;j<n;j=2*j+1){
if(j!=n-1&&A[j]<A[j+1])
j++;
if(temp>A[j]){
break;
}else{
A[index] = A[j];
index = j;
}
}
A[index] = temp;
}
}
希尔排序
import java.util.*;
public class ShellSort {
public int[] shellSort(int[] A, int n) {
// write code here
int feet = n/2;
int index = 0;
while(feet>0){
for(int i=feet;i<n;i++){
index = i;
while(index>=feet){
if(A[index-feet]>A[index]){
int temp = A[index-feet];
A[index-feet] = A[index];
A[index] = temp;
index-=feet;
}else{
break;
}
}
}
feet/=2;
}
return A;
}
}
归并排序
import java.util.*;
public class MergeSort {
public int[] mergeSort(int[] A, int n) {
// write code here
process(A, 0, A.length - 1);
return A;
}
public static void process(int[] arr, int left, int right) {
if (left == right) {
return;
}
int mid = (left + right) / 2;
process(arr, left, mid);
process(arr, mid + 1, right);
merge(arr, left, mid, right);
}
public static void merge(int[] arr, int left, int mid, int right) {
int[] help = new int[right - left + 1];
int l = left;
int r = mid + 1;
int index = 0;
while (l <= mid && r <= right) {
if (arr[l] <= arr[r]) {
help[index++] = arr[l++];
} else {
help[index++] = arr[r++];
}
}
while (l <= mid) {
help[index++] = arr[l++];
}
while (r <= right) {
help[index++] = arr[r++];
}
for (int i = 0; i < help.length; i++) {
arr[left + i] = help[i];
}
}
}