您的位置:首页 > 职场人生

面试准备之---排序

2016-07-26 17:50 369 查看
学习了冒泡排序,选择排序,插入排序,他们的时间复杂度都是o(n^2),归并排序,快速排序,堆排序,希尔排序,他们的时间复杂的都是o(nlogn)

具体学习地址请见    http://www.nowcoder.com/courses/1/2/2

附排序例子代码:

冒泡排序:

import java.util.*;

public class BubbleSort {
public int[] bubbleSort(int[] A, int n) {

// write code here
int temp = 0;
for(int i = 0;i < n-1;i++){
for(int j = 0;j<n-i-1;j++){
if(A [j] > A [j+1]){
temp = A[j];
A[j] = A[j+1];
A[j+1] = temp;
}
}
}
return A;
}
}


选择排序:

import java.util.*;

public class SelectionSort {
public int[] selectionSort(int[] A, int n) {

// 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){
swap(A,i,min);
}
}
return A;
}
void swap(int[] A,int i,int j){
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}

插入排序

import java.util.*;

public class InsertionSort {
public int[] insertionSort(int[] A, int n) {
int i, j, temp;

for(i = 1; i < n; i++){
temp = A[i];
for(j = i; j > 0 && A[j - 1] > temp; j-- ){
A[j] = A[j - 1];
}
A[j] = temp;
}

return A;
}
}


归并排序

import java.util.*;

public class MergeSort {
public int[] mergeSort(int[] A, int n) {
// write code here

sort(A,0,n-1);
return A;
}

public void  sort (int[] arr,int left,int right){
if(left<right)
{
int middle=(left+right)/2;
sort(arr,left,middle);
sort(arr,middle+1,right);
merge(arr,left,middle,right);
}
}

public void merge(int[] arr,int left,int middle,int right){
int i=left;
int j=middle+1;
int k =i;
int[] temp = new int[arr.length];
while(i<=middle&&j<=right){
if(arr[i]<arr[j]){
temp[k++]=arr[i++];
}else{
temp[k++]=arr[j++];
}
}
//剩余未合并的部分
while(i<=middle){
temp[k++]=arr[i++];
}
while(j<=right){
temp[k++]=arr[j++];

}
//将临时数组中的内容存储到原数组中
while(left<=right){
arr[left]=temp[left++];
}

}
}

快速排序

import java.util.*;

public class QuickSort {
public static int[] quickSort(int[] A, int n) {
// write code here
quick(A, 0, n - 1);
return A;
}

private static int[] quick(int[] A, int low, int high) {
// TODO Auto-generated method stub
if (low < high) {
int mid = sort(A, low, high);
quick(A, low, mid-1);
quick(A, mid + 1, high);
}
return A;
}

private static int sort(int[] A, int low, int high) {
// TODO Auto-generated method stub
int key = A[low];
int i = low;
int j = high;
if (low < high) {
while (i < j) {
while (i < j && key <= A[j]) {
j--;
}
if (i < j) {
A[i] = A[j];
}
while (i < j && A[i] <= key) {
i++;
}
if (i < j) {
A[j] = A[i];
}
}
A[i] = key;
}
return i;
}
}
堆排序

import java.util.*;

public class HeapSort {
public int[] heapSort(int[] A, int n) {
// write code here
for(int i = n / 2; i >= 0; i--) {
heapAdjust(A, i, n);
}
for(int i = n - 1; i > 0; i--) {
swap(A, 0, i);
heapAdjust(A, 0, i);
}
return A;
}

void heapAdjust(int[] A, int index, int length) {
int temp = A[index];
for(int j = 2 * index + 1; j < length; j = j * 2 + 1) {
if(j < length - 1 && A[j] < A[j+1]) j++;
if(temp > A[j]) break;
A[index] = A[j];
index = j;
}
A[index] = temp;
}

static void  swap(int[] A,int m,int n){
int temp = A[m];
A[m] = A
;
A
= temp;
}
}


希尔排序

import java.util.*;

public class ShellSort {
public int[] shellSort(int[] A, int n) {
// write code here

if(A==null || n<2)
return A;

int feet = n/2;
int index = 0;
while(feet > 0){

4000
for(int i=feet; i<n; i++){
index = i;
while(index >= feet){
if(A[index-feet] > A[index]){
swap(A,index-feet,index);
index-=feet;
}else{
break;
}
}
}
feet = feet/2;
}
return A;
}

void swap(int[] A,int m,int n){
int temp = A[m];
A[m] = A
;
A
= temp;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: