//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. Node之安装篇

    本篇主要介绍node的安装与相关配置 官网: https://nodejs.org/en/ Linux: Windows:

  2. map遍历性能记录

    map遍历可以通过keySet或者entrySet方式. 性能上:entrySet略胜一筹,原因是keySet获取到key后再根据key去获取value,在查一遍,所以慢一些. keySet: //先 ...

  3. IRepository<Developer> repository 出现 Abp.Domain.Repositories.IRepository which was not registered.

    “/”应用程序中的服务器错误. Can't create component 'SWJ.SSO.DomainServices.TestService' as it has dependencies t ...

  4. 修改Linux的编码集

    使用SSH secure远程连接linux时,查看linux里的内容, 发现乱码.这是由于SSH secure客户端的编码是gbk ,而 linux的默认编码是utf-8 要解决乱码问题,必须将lin ...

  5. POJChallengeRound2 Guideposts 【单位根反演】【快速幂】

    题目分析: 这题的目标是求$$ \sum_{i \in [0,n),k \mid i} \binom{n}{i}G^i $$ 这个形式很像单位根反演. 单位根反演一般用于求:$ \sum_{i \in ...

  6. Memory Layout for Multiple and Virtual Inheritance

    Memory Layout for Multiple and Virtual Inheritance(By Edsko de Vries, January 2006)Warning. This art ...

  7. linux device drivers ch02

    ch02.构造和运行模块 模块的构造: #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE(&qu ...

  8. 转:centos 7 安装音频视频解码器

    (原文:https://blog.csdn.net/zhou1519/article/details/39035233/) 1.安装额外的软件源epel和nux-dextop rpm -Uvh htt ...

  9. Python并发编程之同步\异步and阻塞\非阻塞

    一.什么是进程 进程: 正在进行的一个过程或者说一个任务.而负责执行任务则是cpu. 进程和程序的区别: 程序仅仅只是一堆代码而已,而进程指的是程序的运行过程. 需要强调的是:同一个程序执行两次,那也 ...

  10. 简单的实现HTTP密码验证登陆

    1.首先需要安装 httpd-tools yum install -y httpd-tools 2.安装完成后设置用户名密码,我这里用的是NGINX htpasswd -bc /mypath/ngin ...