//todo

#include<iostream>

void swap(int *a, int *b){int temp = *a; *a = *b; *b = temp;}
void print_array(int *arr, int len) { for (int i = ; i < len; i++) std::cout << arr[i] << " , "; } // swap sort (bubble sort), bubble the largest item to top of list, impractical
void bubble_sort(int arr[], int len)
{
bool need_sort = true;
while (need_sort)
{
need_sort = false;
for (int i = ; i < len - ; i++)
{
if (arr[i] < arr[i + i]){swap(arr+i, arr+i + );need_sort = true;}
}
}
} // selection sort, select the largest one, and put it in the top, just need O(n) insert operation!!!
void select_sort(int arr[], int len)
{
for (int i = ; i < len - ; i++)
{
int index = i;
for (int j = i + ; j < len; j++)
{
if (arr[j] > arr[index]) index = j;
}
swap(arr+i, arr+index);
}
} // the only advantage of both bubble sort and selection sort is easy to implement, should not use it // insert sort, choose item in list one and insert into another in right order
// efficient for small lists and mostly-sorted list
void insert_sort(int arr[], int len)
{
for (int i = ; i < len-; i++)
{
int temp = arr[i + ];
int j = i;
for (; j >= && (arr[j] < temp); j--)
{
arr[j+] =arr[j];
}
if (j < i) arr[j+] = temp;
}
} // as the result list is sorted, insert one new item is same to : put the new item in the top,
// bubble (swap) it to the right position (just need one-time bubbling through the list)
void insert_sort2(int arr[], int len)
{
for (int i = ; i < len; i++)
{
for (int j = i - ; j >= && (arr[j] < arr[j + ]); j--)
{
swap(arr + j, arr + j + );
}
}
} // shell sort, combine insert sort and group (because insert sort is efficient when the list is mostly-sorted,
// one of the fastest algorithms for small number of elements( < 1000 ), and it needs little momery
// unlike efficient sorting algorithms, Shellsort does not require use of the call stack, making it useful in embedded systems where memory is at a premium
void shell_sort(int arr[], int len)
{
for (int gap = len / ; gap >; gap /= )
{
for (int i = ; i < gap; i++)
{
for (int k = i + gap; k < len; k += gap)
{
for (int j = k - gap; j >= && (arr[j] < arr[j + gap]); j -= gap) swap(arr + j, arr + j + gap);
}
}
}
} // merge sort, recursion and double memory of data
void merge_sorted_array(int a[], int lena, int b[], int lenb, int c[])
{
int i(), j(), m();
while (i < lena && j << lenb)
{
if (a[i] < b[j]) c[m++] = a[i++];
else c[m++] = b[j++];
}
while (i < lena) c[m++] = a[i++];
while (j < lenb) c[m++] = b[j++];
} void merge_array(int a[], int first,int mid, int last, int temp[])
{
int i(first), j(mid), m();
while (i < mid && j <=last)
{
if (a[i] < a[j]) temp[m++] = a[i++];
else temp[m++] = a[j++];
}
while (i < mid) temp[m++] = a[i++];
while (j <=last) temp[m++] = a[j++]; for (i = first, m = ; i <= last;) a[i++] = temp[m++];
} void merge_sort(int a[], int first_index, int last_index, int temp[])
{
if (last_index>first_index)
{
int mid = (first_index + last_index) / ;
merge_sort(a, first_index, mid, temp);
merge_sort(a, mid + , last_index, temp);
merge_array(a, first_index, mid+, last_index, temp);
}
} // quick sort, choose a pivot, put all elements smaller before it!
// Typically, quicksort is significantly faster in practice than other Θ(nlogn) algorithms
// in-place, need less swap than merge-sort
void quick_sort(int a[], int first, int last)
{
if (!(first < last)) return;
int i = first, j = last, temp = a[i];
while (i < j)
{
while (i < j && temp < a[j]) j--;
if (i < j) a[i++] = a[j];
while (i < j && a[i] < temp) i++;
if (i < j) a[j--] = a[i];
}
a[i] = temp;
quick_sort(a, first, i - );
quick_sort(a, i + , last);
} int main()
{
int array01[] = { , , , , , , , , };
int temparr[] = { };
quick_sort(array01, ,);
print_array(array01, );
}

sort algorithms的更多相关文章

  1. Basic Sort Algorithms

    1. Bubble Sort public void bubbleSort(int[] arr) { boolean swapped = true; int j = 0; int tmp; while ...

  2. Advanced Sort Algorithms

    1. Merge Sort public class Mergesort { private int[] numbers; private int[] helper; private int numb ...

  3. Javascript数组算法和技巧总结

    Js-arrayMethod https://github.com/HerbertKarajan/Js-arrayMethod List unique an array 数组去重 random str ...

  4. Java性能提示(全)

    http://www.onjava.com/pub/a/onjava/2001/05/30/optimization.htmlComparing the performance of LinkedLi ...

  5. Java Concurrency - invokeAny & invokeAll

    Running multiple tasks and processing the first result A common problem in concurrent programming is ...

  6. AOJ/初等排序习题集

    ALDS1_1_D-MaximumProfit. Codes: //#define LOCAL #include <cstdio> #include <algorithm> u ...

  7. LeetCode Questions List (LeetCode 问题列表)- Java Solutions

    因为在开始写这个博客之前,已经刷了100题了,所以现在还是有很多题目没有加进来,为了方便查找哪些没加进来,先列一个表可以比较清楚的查看,也方便给大家查找.如果有哪些题目的链接有错误,请大家留言和谅解, ...

  8. BookNote: Refactoring - Improving the Design of Existing Code

    BookNote: Refactoring - Improving the Design of Existing Code From "Refactoring - Improving the ...

  9. Lazarus教程 中文版后续给出

    市面上有介绍Delphi的书籍(近来Delphi的书也是越来越少了),但没有一本系统的介绍Lazarus的书,这本书是网上的仅有的一本Lazarus教程,目前全部是英文,不过我已经着手开始翻译,争取尽 ...

随机推荐

  1. mybatis 中 foreach collection的三种用法

    foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index,collection,open,separator,close. ...

  2. 646. Maximum Length of Pair Chain(medium)

    You are given n pairs of numbers. In every pair, the first number is always smaller than the second ...

  3. System.nanoTime与System.currentTimeMillis的区别(转)

    原文地址:http://blog.csdn.net/dliyuedong/article/details/8806868 平时产生随机数时我们经常拿时间做种子,比如用System.currentTim ...

  4. 通过工厂方法配置Bean

    前面几节,我们配过了好多Bean,通过反射机制,在class属性里填写全类名,现在我们来讲讲其他方式,通过工厂方法,还有通过FactoryBean,这个在我们整合第三方框架时会用到. 工厂方法可以分为 ...

  5. Bean的自动装配

    再说自动装配之前,我们先聊一聊什么是手动装配. 手动装配就是我们在先前讲的那些,要自己给定属性,然后赋值 Spring IOC容器可以自动装配Bean,需要做的仅仅实在<bean>的aut ...

  6. CDH Yarn 调度资源指南 - CDH6.0.x 详解

    Yarn 工作架构 最近随着集群大家开始频繁使用集群,资源调度的问题越发的凸显出来.需要更加深入的了解 yarn 资源调度的原理,以及到底在背后做了一些什么事情. 来看一下下面这张图. yarn 里面 ...

  7. excel冻结标题栏,让标题栏不滚动的方法

    https://jingyan.baidu.com/article/148a1921f54afe4d71c3b18e.html

  8. ABP中的模块初始化过程(一)

    在总结完整个ABP项目的结构之后,我们就来看一看ABP中这些主要的模块是按照怎样的顺序进行加载的,在加载的过程中我们会一步步分析源代码来进行解释,从而使自己对于整个框架有一个清晰的脉络,在整个Asp. ...

  9. [模板] 匈牙利算法&&二分图最小字典序匹配

    匈牙利算法 简介 匈牙利算法是一种求二分图最大匹配的算法. 时间复杂度: 邻接表/前向星: \(O(n * m)\), 邻接矩阵: \(O(n^3)\). 空间复杂度: 邻接表/前向星: \(O(n ...

  10. 前端基础之BOM和DOM(响应式布局、计时器、搜索框、select联动)

    一.BOM和DOM JavaScript分为 ECMAScript,DOM,BOM. BOM(Browser Object Model)是指浏览器对象模型,它使 JavaScript 有能力与浏览器进 ...