排序算法分析
来源:互联网 发布:psv如何重新构筑数据库 编辑:程序博客网 时间:2024/04/29 00:33
排序算法一般是笔试面试中经常出现的问题,最近在准备找工作,就整理了一下排序算法的有关知识。
首先附上一张图
这是对各种排序算法的基本介绍。下面我们来逐一分析:
- 插入排序:
- 简单介绍:
插入排序方法分直接插入排序和折半插入排序两种,这里只介绍直接插入排序。插入排序就是每一步都将一个待排数据按其大小插入到已经排序的数据中的适当位置,直到全部插入完毕。 - 代码演示:
- 简单介绍:
public void sort(){ int temp; for(int i = 1; i<arraytoSort.length; i++){ for(int j = i-1; j>=0; j--){ if( arraytoSort[j+1] < arraytoSort[j] ){ temp = arraytoSort[j+1]; arraytoSort[j+1] = arraytoSort[j]; arraytoSort[j] = temp; } } } }
shell排序(希尔排序):
简单介绍:
希尔排序的实质就是分组插入排序,是基于插入排序的一种改进。它通过加大插入排序中元素之间的间隔,并在这些有间隔的元素中进行插入排序,从而使数据项大跨度地移动,当这些数据项排过一趟序之后,希尔排序算法减小数据项的间隔再进行排序,依次进行下去。代码演示:
public void shellSort() { // 计算出最大的h值 int h = 1; while (h <= arraytoSort.length / 3) { h = h * 3 + 1; } while (h > 0) { for (int i = h; i < arraytoSort.length; i += h) { if (arraytoSort[i] < arraytoSort[i - h]) { int tmp = arraytoSort[i]; int j = i - h; while (j >= 0 && arraytoSort[j] > tmp) { arraytoSort[j + h] = arraytoSort[j]; j -= h; } arraytoSort[j + h] = tmp; } } // 计算出下一个h值 h = (h - 1) / 3; } }
- 选择排序:
- 简单介绍:
搜索整个值列,以找到最小值。将该值与值列中第一个位置上的值进行交换。搜索剩下的值列(第一个除外),以找到其中的最小值,然后将其与值列中第二个位置上的值进行交换。对值列中的每个位置重复该过程。在算法结束时,就完成了对值列的排序。 - 代码演示:
- 简单介绍:
public void sort(){ for(int i = 0; i<arraytoSort.length-1; i++){ int min = i; int temp; //find min for(int j = i+1; j<arraytoSort.length ;j++){ if(arraytoSort[j] <arraytoSort[min]){ min = j; } } //swap the min with the ith element temp = arraytoSort[min]; arraytoSort[min] = arraytoSort[i]; arraytoSort[i] = temp; } }
- 堆排序:
- 简单介绍:
堆排序借助了堆这个数据结构,堆类似二叉树,又具有堆积的性质(子节点的关键值总小于(大于)父节点)
堆排序包括两个主要操作:- 保持堆的性质heapify(A,i) 时间复杂度O(logn)
- 建堆 buildmaxheap(A) 时间复杂度O(n)线性时间建堆
- 代码演示:
- 简单介绍:
/** * 最大堆的堆排序 * @author vonnie */public class HeapSort { private int[] heap; private int heapsize; public int times; public HeapSort(int[] a) { heap=a; heapsize=a.length; times=0; } public void BuildMaxHeap() { for(int i=heapsize/2-1;i>=0;i--) { Maxify(i);//依次向上将当前子树最大堆化 } } public void Maxify(int i) { times++; int l=Left(i); times++; int r=Right(i); int largest; times++; if(l<heapsize&&heap[i]<heap[l]) largest=l; else largest=i; times++; if(r<heapsize&&heap[r]>heap[largest]) largest=r; times++; if(largest==i||largest>=heapsize) return; times++; int temp=heap[i]; times++; heap[i]=heap[largest]; times++; heap[largest]=temp; Maxify(largest); } public void MaxHeapSort() { for(int i=0;i<heap.length;i++) { //执行n次,将每个当前最大的值放到堆末尾 times++; int tmp=heap[0]; times++; heap[0]=heap[heapsize-1]; times++; heap[heapsize-1]=tmp; times++; heapsize--; Maxify(0); } } public int[] getSortedArray() { this.BuildMaxHeap(); this.MaxHeapSort(); return heap; } public int Left(int i) { return 2*(i+1)-1; } public int Right(int i) { return 2*(i+1); } public int Parent(int i) { return (i-1)/2; }}
拓展:如果对100亿条数据选择Top k个数据,选择快速排序好还是堆排序好? 答案是只能用堆排序。 堆排序只需要维护一个k大小的空间,即在内存开辟k大小的空间。而快速排序需要开辟能存储100亿条数据的空间。
- 冒泡排序:
- 简单介绍:
冒泡排序是重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端。 - 代码演示:
- 简单介绍:
/** * 冒泡排序: 改进了的冒泡排序,判断第一趟比较后有没有元素交换,没有就说明是排好序的数组,就直接返回 * @param array */ public static void BubbleSort(int[] array) { boolean isSwaped=false; for(int i=0;i<array.length-1;i++) { for(int j=i+1;j<array.length;j++) { if(array[j]<array[i]) { isSwaped=true; int a=array[i]; array[i]=array[j]; array[j]=a; } } if(i==0&&!isSwaped) return; } }
- 快速排序:
- 简单介绍:
选择一个基准元素,通常选择第一个元素或者最后一个元素,通过一趟扫描,将待排序列分成两部分,一部分比基准元素小,一部分大于等于基准元素,此时基准元素在其排好序后的正确位置,然后再用同样的方法递归地排序划分的两部分。 - 代码演示:
- 简单介绍:
/** * 快速排序 * @param array */ public static void QuickSort(int[] array) { times=0; qsort(array, 0, array.length-1); } private static void qsort(int[] arr, int low, int high){ if (low < high){ times++; int pivot=partition(arr, low, high); //将数组分为两部分 qsort(arr, low, pivot-1); //递归排序左子数组 qsort(arr, pivot+1, high); //递归排序右子数组 } } private static int partition(int[] arr, int low, int high){ int pivot = arr[low]; //枢轴记录 while (low<high){ while (low<high && arr[high]>=pivot) { times++; --high; } times++; arr[low]=arr[high]; //交换比枢轴小的记录到左端 while (low<high && arr[low]<=pivot) { times++; ++low; } times++; arr[high] = arr[low]; //交换比枢轴小的记录到右端 } //扫描完成,枢轴到位 times++; arr[low] = pivot; //返回的是枢轴的位置 return low; }
- 归并排序:
- 简单介绍:
归并排序法是将两个(或两个以上)有序表合并成一个新的有序表,即把待排序序列分为若干个子序列,每个子序列是有序的。然后再把有序子序列合并为整体有序序列。 - 代码演示:
- 简单介绍:
/** * 归并排序 * @param array * @param first 第一个元素的index * @param last 最后一个元素的index */ public static int[] MergeSort(int[] array,int first,int last) { times=0; if(first<last) { int count=(first+last)/2; int[] a=Arrays.copyOfRange(array, first, count+1); a=MergeSort(a,0,a.length-1); int[] b=Arrays.copyOfRange(array, count+1, last+1); b=MergeSort(b,0, b.length-1); array=Arrays.copyOf(MergeArray(a,a.length, b,b.length), array.length); } return array; } /** * 合并两个数组 * @param a 排好序的数组 * @param n 数组a的长度 * @param b 排好序的数组 * @param m 数组b的长度 * @return */ public static int[] MergeArray(int[] a,int n,int[] b,int m) { times+=3; int[] array=new int[n+m]; int index=0; int i=0,j=0; while(i<n&&j<m) { times++; array[index++]=(a[i]<b[j]?a[i++]:b[j++]); } if(i<n) { for(;i<n;i++) { times++; array[index++]=a[i]; } } if(j<m) { for(;j<m;j++) { times++; array[index++]=b[j]; } } return array; }
- 基数排序:
- 简单介绍:
将所有待比较数值(正整数)统一为同样的数位长度,数位较短的数前面补零。然后,从最低位开始,依次进行一次排序。这样从最低位排序一直到最高位排序完成以后,数列就变成一个有序序列。 - 代码演示:
- 简单介绍:
public static void radixSort(int[] array){ //首先确定排序的趟数; int max=array[0]; for(int i=1;i<array.length;i++){ if(array[i]>max){ max=array[i]; } } int time=0; //判断位数; while(max>0){ max/=10; time++; } //建立10个队列; LinkQueue<Integer>[] queue=new LinkQueue[10]; for(int i=0;i<10;i++){ queue[i]=new LinkQueue<Integer>(); } //进行time次分配和收集; for(int i=0;i<time;i++){ //分配数组元素; for(int j=0;j<array.length;j++){ //得到数字的第time+1位数; queue[array[j]%(int)Math.pow(10, i+1)/(int)Math.pow(10, i)].enQueue(array[j]); } int count=0;//元素计数器; //收集队列元素; for(int k=0;k<10;k++){ while(queue[k].size()>0){ array[count]=(Integer) queue[k].deQueue().getElement(); count++; } } } }
1 0
- 【算法分析】排序算法
- 内部排序算法分析
- 冒泡排序算法分析!
- 常用排序算法分析
- 排序算法分析
- 常见排序算法分析
- 快速排序算法分析
- 冒泡排序算法分析
- 归并排序算法分析
- 选择排序算法分析
- 堆排序算法分析
- 希尔排序算法分析
- 冒泡排序算法分析
- 排序算法分析
- 排序算法分析
- 排序算法稳定性分析
- 各种排序算法分析
- 插入排序算法分析
- C++Primer第5版读书笔记(第8章)
- PHP + PYTHON 多任务多线程,后台运行,计划任务-实现方法
- Spring --- java定时器,Spring定时器和Quartz定时器
- 端口80 被system PID 4占用的解决办法
- IT_linux_shell_awk_sed_100篇--awk--例子
- 排序算法分析
- Windows 'setspn' 不是内部或外部命令,也不是可运行的程序或批处理文件
- 责任链设计模式(过滤器)
- WebSocket
- javascript-2.function(1)
- 设置后台事务的执行人
- PHP中include和require的区别详解
- java编程中的设计模式
- android-----BroadcastReceiver广播接收器