java堆插入排序_基础排序算法,java实现(快速,冒泡,选择,堆排序,插入)...

1.冒泡排序:

(1)比较相邻的元素。如果第一个比第二个大,就交换他们两个。

(2)外面再套个循环就行。

算法复杂度:O(N2)   不罗嗦,上代码:

//冒泡排序(两两交换,外加一个外循环)

public static void bubbleSort(int sortData[]){

int i,j,temp;

int len = sortData.length;

for (i=0;i

for (j=1;j

if (sortData[j-1] > sortData[j]){

swap(sortData,j-1,j);

}

}

}

}

2. 选择排序

(1)每次从剩下无序中选择最小的元素,进行交换

算法复杂度:O(N2)   不罗嗦,上代码:

//选择排序(从剩下的选最大的交换)

public static void selectionSort(intsortData[]){inti,j,min,index,temp;int len =sortData.length;for (i=0;i

min=sortData[i];

index=i;for (j=i+1;j

min=sortData[j];

index=j;

}

}

swap(sortData,i,index);

}

}

3.插入排序:

(1)基本操作就是将一个数据插入到已经排好序的有序数据中:

//插入排序,把大的往后移动

public static void insertSort(int sortData[]){

int i,j,temp;

int len = sortData.length;

for (i=1;i

temp = sortData[i];

for (j = i-1;j>=0 && temp < sortData[j];j--){

sortData[j+1] = sortData[j];

}

sortData[j+1] = temp;

}

}

3.希尔排序:

是插入排序的改进版,主要是希尔通过引入增量,加强了插入排序的威力。随着增量的减少,就有序了。

//希尔排序

public static void shellSort(intsortData[]){inti,j,temp;int len =sortData.length;int gap = len/2;while(gap>=1){for (i=gap;i

temp=sortData[i];for (j=i-gap;j>=0&&temp

sortData[j+gap] =sortData[j];

}

sortData[j+gap] =temp;

}

gap= gap/2;

}

}

4.堆排序

(1)初始堆  (2)adjust到最小堆或最大堆的格式 (3)排序,根位置和最后一个位置换一下位置,再adjust。

利用最大堆,最小堆这一特性,使得每次从无序的列表中选择最大或者最小的数据。

其基本思想为(大顶堆):

1)将初始待排序关键字序列(R1,R2....Rn)构建成大顶堆,此堆为初始的无序区;

2)将堆顶元素R[1]与最后一个元素R[n]交换,此时得到新的无序区(R1,R2,......Rn-1)和新的有序区(Rn),且满足R[1,2...n-1]<=R[n];

3)由于交换后新的堆顶R[1]可能违反堆的性质,因此需要对当前无序区(R1,R2,......Rn-1)调整为新堆,然后再次将R[1]与无序区最后一个元素交换,得到新的无序区(R1,R2....Rn-2)和新的有序区(Rn-1,Rn)。不断重复此过程直到有序区的元素个数为n-1,则整个排序过程完成。

1)运行时间消耗:构建初始堆 和 重建堆的反复筛选上。

构建堆:

每次从最下层的最右边的非终端节点构建,将他和期孩子节点进行比较,每次最多进行两次比较和交互,因此这个构建时间是N

重建堆(排序):

第i次取堆顶需要logi,那么要去n-1次,所以时间复杂度是nlogn

由于堆排序对原始记录的排序状态并不敏感,因此它无论是最好、最坏和平均时间复杂度均为O(nlogn)。

2) 空间复杂度上,它只有一个用来交换的暂存单元,也算是非常的不错。不过由于记录的比较与交换是跳跃式进行,因此堆排序也是一种不稳定的排序方法。

另外,由于初始构建堆所需的比较次数较多,因此,它并不适合待排序序列个数较少的情况。

public static void heapSort(int sortData []){

buildMaxHeap(sortData);

for(int i=sortData.length-1;i>=1;i--){

swap(sortData,0,i);

maxHeap(sortData,i,0);

}

}

public static void buildMaxHeap(int sortData[]){

int half = sortData.length/2;

for(int i=half;i>=0;i--){

maxHeap(sortData,sortData.length,i);

}

}

public static void maxHeap(int sortData[],int heapSize,int index){

int left = 2*index+1;

int right = 2*index+2;

int largest = index;

if (left < heapSize && sortData[left]>sortData[index]){

largest = left;

}

if(right < heapSize && sortData[right]>sortData[largest]){

largest = right;

}

if (index != largest){

swap(sortData,index,largest);

maxHeap(sortData,heapSize,largest);

}

}

5.  快速排序:

其实是二分法,一般来说以第一个位置做为分界点,把小的放到这个分界点前面,大的放到后面。递归这两部分。

基本思想:选择一个基准元素,通常是选取第一个或者最后一个元素,通过一趟扫描,将待排序的序列分成两部分,一部分比基准元素小,一部分比基准元素大。此时基准元素就在排好序的正确位置。然后采用相同的方法,递归排序划分左右数据。

算法复杂度:平均和最好都是: nlog(n)  最坏情况(逆序): n2         不稳定

//快速排序

public static void quickSort(int sortData[],int start,int end){

if(start >=end) return;

int i=start,j=end,value = sortData[i];

boolean flag = true;

while(i!=j){

if (flag){

if (value > sortData[j]){

swap(sortData,i,j);

flag=false;

}else{

j--;

}

}else{

if (value

swap(sortData,i,j);

flag=true;

}else{

i++;

}

}

}

//printValue(sortData);

quickSort(sortData,start,j-1);

quickSort(sortData,i+1,end);

}

c++

//快排

void qsort(int a[],int low,int high){

if (low >= high) return;

int first = low;

int last = high;

int key = a[first];

while(first

while(first=key) --last;

a[first] = a[last];

while(first

a[last] = a[first];

}

a[first] = key;

qsort(a,low,first-1);

qsort(a,first+1,high);

}

6  归并排序  c++

void merge(int array[],int low,int mid,int high){

int i = low;

int j = mid+1;

int k = 0;

int *array2 = new int [high-low+1];

while(i <= mid && j <= high){

if (array[i] < array[j]){

array2[k] = array[i];

i++;

k++;

}else{

array2[k] = array[j];

j++;

k++;

}

}

while(i <= mid){

array2[k] = array[i];

k++;

i++;

}

while(j <= high){

array2[k] = array[j];

k++;

j++;

}

for(k=0,i=low;i<=high;i++,k++){

array[i] = array2[k];

}

}

void mergeSort(int a[],int low,int high){

if (low

int mid = (low+high)/2;

mergeSort(a,low,mid);

mergeSort(a,mid+1,high);

merge(a,low,mid,high);

}

}

int main(){

int arr [10] = {3,4,2,4,6,2,5,7,0,2};

int len;

ARR_LEN(arr,len);

mergeSort(arr,0,len-1);

for(auto x: arr){

cout << x << " ";

}

}

所有可运行代码:

//gjs

public classsort {public static void printValue(intsortData[]){for (int i = 0;i

System.out.print(sortData[i]+" ");

}

System.out.println();

}public static void swap(int [] data ,int pos1, intpos2){int temp =data[pos1];

data[pos1]=data[pos2];

data[pos2]=temp;

}//冒泡排序(两两交换,外加一个外循环)

public static void bubbleSort(intsortData[]){inti,j,temp;int len =sortData.length;for (i=0;isortData[j]){

swap(sortData,j-1,j);

}

}

}

}//选择排序(从剩下的选最大的交换)

public static void selectionSort(intsortData[]){inti,j,min,index,temp;int len =sortData.length;for (i=0;i

min=sortData[i];

index=i;for (j=i+1;j

min=sortData[j];

index=j;

}

}

swap(sortData,i,index);

}

}//插入排序,把大的往后移动

public static void insertSort(intsortData[]){inti,j,temp;int len =sortData.length;for (i=1;i

temp=sortData[i];for (j = i-1;j>=0 && temp < sortData[j];j--){

sortData[j+1] =sortData[j];

}

sortData[j+1] =temp;

}

}//希尔排序

public static void shellSort(intsortData[]){inti,j,temp;int len =sortData.length;int gap = len/2;while(gap>=1){for (i=gap;i

temp=sortData[i];for (j=i-gap;j>=0&&temp

sortData[j+gap] =sortData[j];

}

sortData[j+gap] =temp;

}

gap= gap/2;

}

}public static void heapSort(intsortData []){

buildMaxHeap(sortData);for(int i=sortData.length-1;i>=1;i--){

swap(sortData,0,i);

maxHeap(sortData,i,0);

}

}public static void buildMaxHeap(intsortData[]){int half = sortData.length/2;for(int i=half;i>=0;i--){

maxHeap(sortData,sortData.length,i);

}

}public static void maxHeap(int sortData[],int heapSize,intindex){int left = 2*index+1;int right = 2*index+2;int largest =index;if (left < heapSize && sortData[left]>sortData[index]){

largest=left;

}if(right < heapSize && sortData[right]>sortData[index]){

largest=right;

}if (index !=largest){

swap(sortData,index,largest);

maxHeap(sortData,heapSize,largest);

}

}//快速排序

public static void quickSort(int sortData[],int start,intend){if (start >=end) return;int i=start,j=end,value =sortData[i];boolean flag = true;while(i!=j){if(flag){if (value >sortData[j]){

swap(sortData,i,j);

flag=false;

}else{

j--;

}

}else{if (value

swap(sortData,i,j);

flag=true;

}else{

i++;

}

}

}//printValue(sortData);

quickSort(sortData,start,j-1);

quickSort(sortData,i+1,end);

}public static voidmain(String[] args) {int [] a={2,4,1,5,8,9,43,3,7,8,9,0,3};

printValue(a);//bubbleSort(a);//selectionSort(a);//insertSort(a);//heapSort(a);//shellSort(a);

quickSort(a,0,a.length-1);

printValue(a);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值