Advanced Sort Algorithms
1. Merge Sort
public class Mergesort {
private int[] numbers;
private int[] helper;
private int number;
public void sort(int[] values) {
this.numbers = values;
number = values.length;
this.helper = new int[number];
mergesort(0, number - 1);
}
private void mergesort(int low, int high) {
if (low < high) {
int middle = low + (high - low) / 2;
mergesort(low, middle);
mergesort(middle + 1, high);
merge(low, middle, high);
}
}
private void merge(int low, int middle, int high) {
for (int i = low; i <= high; i++) {
helper[i] = numbers[i];
}
int i = low;
int j = middle + 1;
int k = low;
while (i <= middle && j <= high) {
if (helper[i] <= helper[j]) {
numbers[k] = helper[i];
i++;
} else {
numbers[k] = helper[j];
j++;
}
k++;
}
while (i <= middle) {
numbers[k] = helper[i];
k++;
i++;
}
}
}
Performance
Worst case performance O(n log n)
Best case performance O(n log n) typical, O(n) natural variant
Average case performance O(n log n)
Worst case space complexity O(n) auxiliary
2. Shell Sort
public static void main(String[] args) {
int[]a={49,38,65,97,76,13,27,49,78,34,12,64,1};
System.out.println("Beforeing:");
for(int i=0;i<a.length;i++){
System.out.print(a[i]+"");
}
int d=a.length;
while(true) {
d=d/2;
for(int x=0;x<d;x++){
for(int i=x+d;i<a.length;i=i+d){
int temp=a[i];
int j;
for(j=i-d;j>=0&&a[j]>temp;j=j-d){
a[j+d]=a[j];
}
a[j+d]=temp;
}
}
if(d==1){
break;
}
}
System.out.println();
System.out.println("Aftering:");
for(inti=0;i<a.length;i++){
System.out.print(a[i]+"");
}
}
Performance
Worst case performance O(n2)
Best case performance O(n log n)
Average case performance depends on gap sequence
Worst case space complexity О(n) total, O(1) auxiliary
Comparison
The Shellsort is good for medium-sized arrays, perhaps up to a few thousand items, depending on the particular implementation. It’s not quite as fast as quicksort and other O(N*logN) sorts, so it’s not optimum for very large files. However, it’s much faster than the O(N2) sorts like the selection sort and the insertion sort, and it’s very easy to implement.
3. Quick Sort
public class Quicksort {
private int[] numbers;
private int number;
public void sort(int[] values) {
if (values ==null || values.length==0){
return;
}
this.numbers = values;
number = values.length;
quicksort(0, number - 1);
}
private void quicksort(int low, int high) {
int i = low, j = high;
int pivot = numbers[low + (high-low)/2];
while (i <= j) {
while (numbers[i] < pivot) {
i++;
}
while (numbers[j] > pivot) {
j--;
}
if (i <= j) {
exchange(i, j);
i++;
j--;
}
}
if (low < j)
quicksort(low, j);
if (i < high)
quicksort(i, high);
}
private void exchange(int i, int j) {
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
}
Performance
Worst case performance O(n2)
Best case performance O(n log n) (simple partition) or O(n) (three -way partition and equal keys)
Average case performance O(n log n)
Worst case space complexity O(n) auxiliary (naive) O(log n) auxiliary (Sedgewick 1978)
Comparison
Quicksort is undoubtedly the most popular sorting algorithm, and for good reason: In the majority of situations, it’s the fastest, operating in O(N*logN) time. (This is only true for internal or in-memory sorting; for sorting data in disk files, other algorithms may be better.)
4. Radix Sort
public void radixsort(int[] input) {
final int RADIX = 10;
List<Integer>[] bucket = new ArrayList[RADIX];
for (int i = 0; i < bucket.length; i++) {
bucket[i] = new ArrayList<Integer>();
}
boolean maxLength = false;
int tmp = -1, placement = 1;
while (!maxLength) {
maxLength = true;
for (Integer i : input) {
tmp = i / placement;
bucket[tmp % RADIX].add(i);
if (maxLength && tmp > 0) {
maxLength = false;
}
}
int a = 0;
for (int b = 0; b < RADIX; b++) {
for (Integer i : bucket[b]) {
input[a++] = i;
}
bucket[b].clear();
}
placement *= RADIX;
}
}
Performance
Worst case performance O(kN)
Worst case space complexity O(k + N)
Comparison
Of course, like mergesort, the radix sort uses about twice as much memory as quicksort. It’s generally true that if you have more data items, you’ll need longer keys. If you have 10 times as much data, you may need to add another digit to the key. The number of copies is proportional to the number of data items times the number of digits in the key. The number of digits is the log of the key values, so in most situations we’re back to O(N*logN) efficiency, the same as quicksort.
Advanced Sort Algorithms的更多相关文章
- Basic Sort Algorithms
1. Bubble Sort public void bubbleSort(int[] arr) { boolean swapped = true; int j = 0; int tmp; while ...
- sort algorithms
//todo #include<iostream> void swap(int *a, int *b){int temp = *a; *a = *b; *b = temp;} ; i &l ...
- Java性能提示(全)
http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLi ...
- Machine Learning - 第3周(Logistic Regression、Regularization)
Logistic regression is a method for classifying data into discrete outcomes. For example, we might u ...
- [C4] Andrew Ng - Improving Deep Neural Networks: Hyperparameter tuning, Regularization and Optimization
About this Course This course will teach you the "magic" of getting deep learning to work ...
- [C2P2] Andrew Ng - Machine Learning
##Linear Regression with One Variable Linear regression predicts a real-valued output based on an in ...
- [C2P3] Andrew Ng - Machine Learning
##Advice for Applying Machine Learning Applying machine learning in practice is not always straightf ...
- Bayesian machine learning
from: http://www.metacademy.org/roadmaps/rgrosse/bayesian_machine_learning Created by: Roger Grosse( ...
- Javascript数组算法和技巧总结
Js-arrayMethod https://github.com/HerbertKarajan/Js-arrayMethod List unique an array 数组去重 random str ...
随机推荐
- This Handler class should be static or leaks might occur(null) 解决办法 (转)
原文链接:http://blog.csdn.net/wuleihenbang/article/details/17126371 首先解释下这句话This Handler class should be ...
- jquery处理checkbox(复选框)是否被选中
现在如果一个复选框被选中,是用checked=true,checked="checked"也行 要用prop代替attr会更好,虽然在jQuery1.6之前版本的attr()方法能 ...
- Entity Framework Code First在Oracle下的伪实现
为什么要说是伪实现,因为还做不到类似MsSql中那样完全的功能.Oralce中的数据库还是要我们自己手动去创建的.这里,我们舍掉了Model First中的EDMX文件,自己在代码里面写模型与映射关系 ...
- Unity3D C#中使用LINQ查询(与 SQL的区别)
学过SQL的一看就懂 LINQ代码很直观 但是,LINQ却又跟SQL完全不同 首先来看一下调用LINQ的代码 int[] badgers = {36,5,91,3,41,69,8}; var skun ...
- 原生JavaScript实现焦点图轮播
不管是高校的网站还是电商的页面,焦点图的切换和轮播应该是一项不可或缺的应用.今天把焦点图轮播制作的技术要点做下笔记,以供日后查看. 一.结构层(HTML) 焦点图的HTML结构很简单,就是一个父容器( ...
- Java -- 浅入Java反射机制
1,Java 反射是Java语言的一个很重要的特征,它使得Java具体了"动态性". Java 反射机制主要提供了以下功能: 在运行时判断任意一个对象所属的类. 在运行时构造任意一 ...
- 利用 force recovery 解决服务器 crash 导致 MySQL 重启失败的问题
小明同学在本机上安装了 MySQL 5.7.17 配合项目进行开发,并且已经有了一部分重要数据.某天小明在开发的时候,需要出去一趟就直接把电脑关掉了,没有让 MySQL 正常关闭,重启 MySQL 的 ...
- 傻瓜式使用AutoFac
定义一个接口: using System; using System.Collections.Generic; using System.Linq; using System.Web; namespa ...
- HYML / CSS和Javascript 部分
1 CSS实现垂直水平居中 HTML结构: <div class="wrapper"> <div class="content">&l ...
- Visual Studio(VS) F12 查看DLL源代码
前言 我在VS中调试某个函数时,突发奇想"能不能使用VS的F12(转到定义)查看这个dll中当前函数的实现(源码),而不是像VS自带功能那样只能看到函数名和参数?" 回想起来在安装 ...