Insertion sort, select sort, Quick sort and Merge sort
来源:互联网 发布:韩国tvn软件下载 编辑:程序博客网 时间:2024/05/29 04:43
<pre name="code" class="java">//插入排序/*一般来说,插入排序都采用in-place在数组上实现。具体算法描述如下:从第一个元素开始,该元素可以认为已经被排序取出下一个元素,在已经排序的元素序列中从后向前扫描如果该元素(已排序)大于新元素,将该元素移到下一位置重复步骤3,直到找到已排序的元素小于或者等于新元素的位置将新元素插入到该位置后重复步骤2~5如果比较操作的代价比交换操作大的话,可以采用二分查找法来减少比较操作的数目。该算法可以认为是插入排序的一个变种,称为二分查找插入排序。*/public class Insertion { public static void insertionSort(Comparable []data){ for(int index=1;index<data.length;index++){ Comparable key = data[index]; int position = index; //shift larger values to the right while(position>0&&data[position-1].compareTo(key)>0){ data[position] = data[position-1]; position--; } data[position]=key; } } public static void main(String []args){ Comparable []c={4,9,23,1,45,27,5,2}; insertionSort(c); for(int i=0;i<c.length;i++) System.out.println("插入排序:"+c[i]); } }/*算法复杂度如果目标是把n个元素的序列升序排列,那么采用插入排序存在最好情况和最坏情况。最好情况就是,序列已经是升序排列了,在这种情况下,需要进行的比较操作需(n-1)次即可。最坏情况就是,序列是降序排列,那么此时需要进行的比较共有n(n-1)/2次。插入排序的赋值操作是比较操作的次数减去(n-1)次。平均来说插入排序算法复杂度为O(n2)。因而,插入排序不适合对于数据量比较大的排序应用。但是,如果需要排序的数据量很小,例如,量级小于千,那么插入排序还是一个不错的选择。 插入排序在工业级库中也有着广泛的应用,在STL的sort算法和stdlib的qsort算法中,都将插入排序作为快速排序的补充,用于少量元素的排序(通常为8个或以下)。*///快速排序public class quickSort { inta[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};public quickSort(){ quick(a); for(int i=0;i<a.length;i++) System.out.println(a[i]);}publicint getMiddle(int[] list, int low, int high) { int tmp = list[low]; //数组的第一个作为中轴 while (low < high) { while (low < high && list[high] >= tmp) { high--; } list[low] = list[high]; //比中轴小的记录移到低端 while (low < high && list[low] <= tmp) { low++; } list[high] = list[low]; //比中轴大的记录移到高端 } list[low] = tmp; //中轴记录到尾 return low; //返回中轴的位置 } publicvoid _quickSort(int[] list, int low, int high) { if (low < high) { int middle = getMiddle(list, low, high); //将list数组进行一分为二 _quickSort(list, low, middle - 1); //对低字表进行递归排序 _quickSort(list, middle + 1, high); //对高字表进行递归排序 } } publicvoid quick(int[] a2) { if (a2.length > 0) { //查看数组是否为空 _quickSort(a2, 0, a2.length - 1); } } }
0 0
- Insertion sort, select sort, Quick sort and Merge sort
- Bubble sort, Insertion sort, merge sort, quick sort, heap sort
- Insertion Sort and Merge Sort
- Insertion sort and Merge sort
- Insertion Sort and Merge Sort
- Insertion Sort, Merge Sort
- Quick Sort & Binary Merge Sort
- Quick sort VS Merge sort
- Sort List (merge & Insertion)
- Bubble, Insertion, Selection, Quick and Heap Sort
- Inserting Sort, Shell Sort, Heap Sort and Quick Sort
- Quick Sort and Random Quick Sort
- Merge Sort Array --> Merge Sort Linked List --> Insertion Sort Array --> Insertion sort Linked List
- MergeSort and Quick Sort
- Insertion Sort
- Insertion Sort
- insertion sort
- insertion sort
- Leetcode NO.104 Maximum Depth of Binary Tree
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) C题
- 自制操作系统 第3步
- [转]内容匹配广告投放技术
- 用来获取页面背景的书签(页面背景是DataURI)
- Insertion sort, select sort, Quick sort and Merge sort
- iOS复习笔记14:常用数据结构之类
- jquery 获取 自定义属性(attr 和 prop)
- ACM_switch case的使用
- Android Studio汉化版安装说明
- Dependency Walker
- 浅谈 Redis 数据库的键值设计
- QT5+MSVC2012环境下可能出现的各种问题及解决方法
- 开发笔记 (2) :redis 数据库结构设计