冒泡排序:O(n^2)
for(int i=0;i<arr.length-1;i++){
for(int j=0;j<arr.length-1-i;j++){
if(arr[j] > arr[]){
int t = arr[j];
arr[j] = arr[j+1];
arr[j+1] = arr[j];
}
}
}
选择排序:O(n^2)
int index,t;
for(int i=0;i<arr.length-1;i++){
index = i;
for(int j=i+1;j<arr.length;j++){
if(arr[index] > arr[j]){
index = j;
}
}
t = arr[i];
arr[i] = arr[index];
arr[index] = t;
}
插入排序:O(n^2)
int preIndex,current;
for(int i=1;i<arr.length;i++){
preIndex = i-1;
current = arr[i];
while(preIndex >= 0 && arr[preIndex]>current){
arr[preIndex + 1] = arr[preIndex];
preIndex--;
}
arr[preIndex + 1] = current;
}
希尔排序:
for(int i=Math.floor(arr.length/2);i>0;i = Math.floor(i/2)){
for(int j=i;j<arr.length;j++){
int k = i;
int current = arr[i];
while(k-i >= 0 && arr[k-i] > arr[k]){
arr[k] = arr[k-i];
k = k-i;
}
arr[k] = current;
}
}
归并排序:
public int[] mergeSort(int[] arr,int left,int right){
int mid = (left + right) /2;
if(left < right){
mergeSort(arr,left,middle);
mergeSort(arr,middle+1,right);
//左右归并
merge(arr,low,middle,right);
}
return arr;
}
public static merge(int[] arr,int left,int middle,int right){
int[] result = new int[left + right];
int i = left;
int j = middle + 1;
int index = 0;
while(i<=middle && j<=right){
if(arr[i] <= arr[j]){
result[index++] = arr[i++];
}else{
result[index++] = arr[j++];
}
}
while(i<=middle){
result[index++] = arr[i++];
}
while(j<=right){
result[index++] = arr[j++];
}
for(int x=0;x<result.length;x++){
arr[x+left] = result[x];
}
}
快速排序:
public int[] quickSort(int[] arr){
if(arr.length>0){
quickSort(arr,0,arr.length-1);
}
}
private int[] quickSort(int[] arr,int low,int high){
if(low<high){
int middle = getMiddle(arr,low,high);
quickSort(arr,low,middle-1);
quickSort(arr,middle+1,high);
}
return arr;
}
public int getMiddle(int[] arr,int low,int high){
int temp = arr[low];
while(low<high){
while(low<high && arr[high] > temp){
j--;
}
arr[low] = arr[high];
while(i<j && arr[i] <= temp){
i++;
}
arr[high] = arr[low];
}
arr[low] = temp;
return low;
}
堆排序:
int len;
public void buildHeap(int[] arr){//建立大顶堆
int len = arr.length;
for(int i = len/2;i>=0;i--){
heapify(arr,i);
}
}
private void heapify(int[] arr,int i){
int left = 2 * i + 1;
int right = 2 * i + 2;
int largest = i;
if(left < arr.length && arr[left] > arr[largest]){
largest = left;
}
if(right< arr.length && arr[right] > arr[largest]){
largest = right;
}
if(largest != i){
swap(arr,i,largest);
heapify(arr,largest);
}
}
private void swap(int[] arr,int first,int second){
int t = arr[first];
arr[first] = arr[second];
arr[second] = t;
}
public int[] heapSort(int[] arr){
buildHeap(arr);
for(int i = arr.length-1;i>0;i--){
swap(arr,0,i);
len--;
heapify(arr,0);
}
return arr;
}
计数排序:
- 找出待排序的数组中最大和最小的元素;
- 统计数组中每个值为i的元素出现的次数,存入数组C的第i项;
- 对所有的计数累加(从C中的第一个元素开始,每一项和前一项相加);
- 反向填充目标数组:将每个元素i放在新数组的第C(i)项,每放一个元素就将C(i)减去1。
public int[] countingSort(int[] arr,int maxValue){
int[] c = new int[maxValue+1];
int[] b = new int[arr.length],sum = 0;
for(int i = 0;i<arr.length;i++){
c[arr[i]]++;// 统计A中各元素个数,存入C数组
}
for(int i = 0;i<c.length;i++){//修改C数组
sum += c[i];
c[i] = sum;
}
for(int i = arr.length-1;i>=0;i--){
b[c[arr[i]]-1] = arr[i];//将A中该元素放到排序后数组B中指定的位置
c[array[i]]--;//将C中该元素-1,方便存放下一个同样大小的元素
}
return b;
}
桶排序:
- 设置一个定量的数组当作空桶;
- 遍历输入数据,并且把数据一个一个放到对应的桶里去;
- 对每个不是空的桶进行排序;
- 从不是空的桶里把排好序的数据拼接起来。
int bucketSize = 10;
public void bucketSort(int[] arr){
List<Integer> bucket[] = new ArrayList[bucketSize];
for(int i = 0;i<arr.length;i++){
int temp = arr[i] / 10000;
if(bucket[temp] == null){
bucket[temp] = new ArrayList<Integer>();
}
bucket[temp].add(arr[i]);
}
for(int i = 0;i<bucket.length;i++){
Collections.sort(bucket[i]);
for(int j = 0;j<bucket[i].size();j++){
System.out.print(bucket[i].get(j) + “\t”);
}
}
}