首页 > 办公教程 > 正文

希尔排序java,冒泡、直插、选择、快速、希尔、归并排序算法进行比较

2020-05-20 09:42:55  来源:http://www.capsicumpm.com  编辑:admin

给出1000个通过随机生成的数据,分别用简单选择排序,希尔排序法,堆排序法进行排序

//这是我目前所知道的所有排序了整合了一下:我测试用的是20个数字,你把数量改成1000即可!importjava.util.Scanner;publicclassTestSort{privatestaticScannersc=newScanner(System.in);privatestaticHeapSortmyHs=newHeapSort();publicstaticvoidmain(String[]args){int[]arr=newint[20];//数量!for(inti=0;i<arr.length;i++){arr[i]=(int)(Math.random()*10+1);}sop("原始顺序:\n");for(inta:arr)sop(a+",");sop("\n\n----------------请选择需要使用的方法----------------\n");sop("\n-----1:冒泡;2:选择;3:插排;4:快排;5:希尔;6:堆排------\n");inttem=sc.nextInt();window(arr,tem);sop("\n排序后:\n");for(inta:arr)sop(a+",");}//菜单!privatestaticvoidwindow(intarr[],inta){switch(a){case1:myBubble(arr);break;case2:mySelect(arr);break;case3:myInsert(arr);break;case4:quicksort(arr);break;case5:myshell(arr);break;default:myHs.buildMaxHeapify(arr);myHs.heapSort(arr);break;}}//冒泡!privatestaticvoidmyBubble(int[]arr){for(inti=0;i<arr.length-1;i++)for(intj=0;j<arr.length-i-1;j++)if(arr[j]>arr[j+1])swp(arr,j,j+1);}//选择!privatestaticvoidmySelect(int[]arr){for(inti=0;i<arr.length-1;i++)for(intj=i+1;j<arr.length;j++)if(arr[i]>arr[j])swp(arr,i,j);}//插排!privatestaticvoidmyInsert(int[]arr){for(inti=1;i<arr.length;i++)for(intj=i;j>0;j--)if(arr[j]<arr[j-1])swp(arr,j,j-1);}//快排!publicstaticvoidquicksort(int[]arr){intstart=0;intend=arr.length-1;quickSort(arr,start,end);}publicstaticvoidquickSort(int[]arr,intstart,intend){if(start<end){intbase=arr[start];//inti=start,j=end;do{while((arr[i]<base)&&(i<end))i++;while((arr[j]>base)&&(j>start))j--;if(i<=j){swp(arr,i,j);i++;j--;}}while(i<=j);if(start<j)quickSort(arr,start,j);if(end>i)quickSort(arr,i,end);}}//希尔privatestaticvoidmyshell(int[]arr){inti,j,gap,n;n=arr.length;for(gap=n/2;gap>0;gap/=2)for(i=gap;i<n;i++)for(j=i-gap;j>=0&&arr[j]>arr[j+gap];j-=gap){inttem=arr[j];arr[j]=arr[j+gap];arr[j+gap]=tem;}}//堆!privatestaticclassHeapSort{voidbuildMaxHeapify(int[]data){intstartIndex=getParentIndex(data.length-1);for(inti=startIndex;i>=0;i--){maxHeapify(data,data.length,i);}}voidmaxHeapify(int[]data,intheapSize,intindex){intleft=getChildLeftIndex(index);intright=getChildRightIndex(index);intlargest=index;if(left<heapSize&&data[index]<data[left]){largest=left;}if(right<heapSize&&data[largest]<data[right]){largest=right;}if(largest!=index){inttemp=data[index];data[index]=data[largest];data[largest]=temp;maxHeapify(data,heapSize,largest);}}privatevoidheapSort(int[]data){for(inti=data.length-1;i>0;i--){inttemp=data[0];data[0]=data[i];data[i]=temp;maxHeapify(data,i,0);}}privateintgetParentIndex(intcurrent){return(current-1)>>1;}privateintgetChildLeftIndex(intcurrent){return(current<<1)+1;}privateintgetChildRightIndex(intcurrent){return(current<<1)+2;}}//交换!privatestaticvoidswp(int[]arr,inti,intj){inttem=arr[i];arr[i]=arr[j];arr[j]=tem;}//打印!privatestatic<T>voidsop(Tt){System.out.print(t);}}

求一个希尔排序法的代码

#include <stdio.h> #include <stdlib.h>#include <time.h>#define MAX 100#define SWAP(x,y) {int t; t = x; x = y; y = t;}void shellsort(int[]);int main(void) {int number[MAX] = {0};int i;srand(time(NULL));printf("排序前:");for(i = 0; i < MAX; i++) {number[i] = rand() % 100;printf("%d ", number[i]);}printf("\n");shellsort(number);return 0;}void shellsort(int number[]) {int i, j, k, gap, t;gap = MAX / 2;while(gap > 0) {for(k = 0; k < gap; k++) {for(i = k+gap; i < MAX; i+=gap) {for(j = i - gap; j >= k; j-=gap) {if(number[j] > number[j+gap]) {SWAP(number[j], number[j+gap]);}elsebreak;}}}printf("\ngap = %d:", gap);for(i = 0; i < MAX; i++)printf("%d ", number[i]);printf("\n");gap /= 2;}}

编写程序实现直接插入排序、希尔排序(d=5)、直接选择排序、快速排序和二路归并排序算法。 西南科技大学oj

<% Set RSNEXT=Conn.Execute("SELECT * FROM bbs WHERE [topnum]<>ID AND opnum='" &RS(0)& "' ORDER BY ID ASC")

java中希尔排序算法代码

publicclassShellSort{//交换数组元素privatestaticvoidswap(int[]a,inti,intj){intt=a[i];a[i]=a[j];a[j]=t;}publicstaticvoidsort(int[]a){inth=1;while(h<a.length/3){//寻找合适的间隔hh=3*h+1;}while(h>=1){//将数组变为间隔h个元素有序for(inti=h;i<a.length;i++){//间隔h插入排序for(intj=i;j>=h&&a[j]<a[j-h];j-=h){swap(a,j,j-h);}}h/=3;}}}

Java冒泡排序是怎么写的?

你好!希望对你有帮助!本回答被网友采纳