//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. c++入门之函数指针和函数对象

    函数指针可以方便我们调用函数,但采用函数对象,更能体现c++面向对象的程序特性.函数对象的本质:()运算符的重载.我们通过一段代码来感受函数指针和函数对象的使用: int AddFunc(int a, ...

  2. Django rest framework(8)---- 视图和渲染器

    django rest framework 之视图 序列化器    PagerSerialiser from rest_framework import serializers from api im ...

  3. USB安装centos6系统(centos7需要换软件)

    一.下载系统镜像 二.下载安装软碟通软件UltraISO 三.插入U盘制作启动盘 1.用软碟通打开镜像文件:文件-->打开 2.写入映像:启动-->写入硬盘映像 3.等待写入完成 四.系统 ...

  4. 图像压缩编解码实验(DCT编码+量化+熵编码(哈夫曼编码))【MATLAB】

    课程要求 Assignment IV Transform + Quantization + Entropy Coding Input: an intra-frame or a residue pict ...

  5. monkey事件简介

    操作事件简介 Monkey所执行的随机事件流中包含11大事件,分别是触摸事件.手势事件.二指缩放事件.轨迹事件.屏幕旋转事件.基本导航事件.主要导航事件.系统按键事件.启动Activity事件.键盘事 ...

  6. js对象取值的两种方式

    :"李四"}; var v1 = obj.name1; //张三, 使用点的方式 //报错,不能使用点的方式 ]; //李四,使用中括号的方式 var key = "na ...

  7. Microsoft Visual Studio Tools for AI

    https://www.visualstudio.com/zh-hans/downloads/ai-tools-vs/ 开发.调试和部署深度学习和 AI 解决方案 Visual Studio Tool ...

  8. vue之——从彩笔的进步之路

    因为这个文章开的有点晚,不可能说从头教学vue的使用,所以大概还是记录一下我的学习路线吧: 一开始是想学一个前端框架,最后选择了vue,一开始是看了表严肃的vue课程,b站有,讲的相当好,就算打个小广 ...

  9. A.02.01—功能定义—一般定义

    二章将属于较轻松的内容,整个过程也会主要以文字描述为主. 最常见的功能定义为按使用操作来定义,如下面的例子是最普通的: 1)用户将雨刮开关打至高速档,雨刮以高速速率刮刷 2)在电源档位为OFF时,用户 ...

  10. Linux安装Tomcat8

    前置条件 安装jdk,见参考文章 下载Tomcat8 先从tomcat网站上下载最新的.gz安装包 tomcat官网下载地址 在下面找到Linux对应的tomcat安装包 我下载的文件名是:apach ...