Notice : these algorithms achieved by Java.

So,let's going to it.

firstly, what is Bubblesort? why we call it in this name?

emmmm.... Maybe this image will give you a clear understanding.

          Bubblesort.jpg

I meet so many descriptions,and they all have a common that is : the key which in sorting will end in it's final position!

Bubblesort is stable

there is code:

import java.util.Arrays;

public class BubbleSort{
public static void sort(int[] arr){
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr.length-i-1;j++){
if(arr[j]>arr[j+1]){
int temp=arr[j+1];
arr[j+1]=arr[j];
arr[j]=temp;
}
}
}
}
public static void main(String [] args){
int[] ints={5,3,4,1,2};
sort(ints);
System.out.println(Arrays.toString(ints));
}
}

Secondly,about Insertsort

This sorting is find the appropriate position. when a key is low in front ,they will exchange.

code:

import java.util.Arrays;
public class InsertSort{
public static void sort(int[] arr){
int temp;
for(int i=0;i<arr.length;i++){
for(int j=0;j<i;j++){
if(arr[i]<arr[j]){ temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}
public static void main(String [] args){
int [] ints={5,3,4,1,2};
sort(ints);
System.out.println(Arrays.toString(ints));
}
}

Thirdly,the Mergesort.

1,Request space to be the sum of two sorted sequences, which are used to store merged sequences

2,Set two pointers at the beginning of two sorted sequences

3,Compare the elements pointed by the two pointers, select the relatively small elements and place them in the merge space, and move the pointer to the next location.

4,Repeat step 3 until a pointer reaches the end of the sequence

5,Copy all the remaining elements of another sequence directly to the end of the merged sequence

code:

import java.util.Arrays;

public class MergeSort{
public static void mergeSort(int [] arrays,int left,int right){
//如果数组还可以拆分
if(left<right)
{
//数组的中间位置
int middle=(left+right)/2;
//拆分左边数组
mergeSort(arrays,left,middle);
//拆分右边数组
mergeSort(arrays,middle+1,right);
//合并
merge(arrays,left,middle,right);
}
}
/*
合并数组
*/
public static void merge(int [] arr,int left,int middle,int right){
//申请合并空间 大小为两个已经排序序列之和
int [] temp=new int[right-left+1];
//i和j为两个已经排序号的数组的起始位置
int i=left;
int j=middle+1;
int k=0;
while(i<=middle&&j<=right){
//将比较小的数组放入合并空间
if(arr[i]<arr[j]){
temp[k++]=arr[i++]; }else{
temp[k++]=arr[j++];
}
}
//将左边剩余元素写入合并空间
while(i<=middle){
temp[k++]=arr[i++]; }
//将右边剩余元素写入合并空间
while(j<=right){
temp[k++]=arr[j++];
}
//将排序后的数组协会原来的数组
for(int l=0;l<temp.length;l++){
arr[l+left]=temp[l];
}
}
public static void main(String [] args){
int[] ints={5,3,4,1,2};
mergeSort(ints,0,ints.length-1);
System.out.println(Arrays.toString(ints));
}
}

The most important sorting :Quicksort

Quicksort , also known as partition-exchange sort, is short for Quicksort, a sort algorithm, first proposed by Tony Hall. On average, the order of n items is O (nlogn) times. In the worst case, O (n ^ 2) comparisons are required, but this is not common. In fact, Quick Sorting O (nlogn) is usually significantly faster than other algorithms because its inner loop can be achieved efficiently on most architectures.

step:

Pick out an element from a sequence called pivot.

Rearrange the sequence. All elements smaller than the benchmark are placed in front of the benchmark, and all elements larger than the benchmark are placed behind the benchmark (the same number can go to any side). After this segmentation, the benchmark is in the middle of the sequence. This is called a partition operation.

Recursively, the subordinate sequence of elements smaller than the base value and those larger than the base value is sorted.

code:

import java.util.Arrays;

public class QuickSort{
public static void sort(int[] arr,int head,int tail){
if(head>=tail||arr==null||arr.length<=1){
return;
}
//设置数组的起始位置i 结束位置j 基准pivot为数组的中间
int i=head,j=tail,pivot=arr[(head+tail)/2];
while(i<=j){
//当数组小于基准 循环结束后 相当于i所处的位置的值为大于基准的元素
while(arr[i]<pivot){
++i;
}
//当数组大于基准 循环结束后 相当于j所处的位置的值为小于基准的元素
while(arr[j]>pivot){
--j;
}
//如果i<j 那么将交换i j对应位置的值
if(i<j){
int t=arr[i];
arr[i]=arr[j];
arr[j]=t;
//指针继续移动
++i;
--j;
}else if(i==j){
//如果i=j,那么说明本次排序已经结束,将i++ 如果这里不使用i++ 那么后面的sort(arr,i,tial)将改为sort(arr,i+1,tail)
++i;
}
}
//继续将数组分割
sort(arr,head,j);
sort(arr,i,tail);
} public static void main(String[] args){
int[] ints={5,3,4,1,2};
sort(ints,0,ints.length-1);
System.out.println(Arrays.toString(ints));
}
}

The algorithm learning of sort which include Bubblesort,Insertsort,Quicksort and Mergesort.的更多相关文章

  1. C++ algorithm 里的sort函数应用

    MSDN中的定义: template<class RanIt>    void sort(RanIt first, RanIt last); //--> 1)template< ...

  2. <algorithm>里的sort函数对结构体排序

    题目描述 每天第一个到机房的人要把门打开,最后一个离开的人要把门关好.现有一堆杂乱的机房签到.签离记录,请根据记录找出当天开门和关门的人. 输入描述: 每天的记录在第一行给出记录的条目数M (M &g ...

  3. STL下<algorithm>下的sort函数

    定义: sort函数用于C++中,对给定区间所有元素进行排序,默认为升序,也可进行降序排序.sort函数进行排序的时间复杂度为nlog2n,比冒泡之类的排序算法效率要高,sort函数包含在头文件为#i ...

  4. HDOJ2037 今年暑假不AC (经典的贪心问题)

    Description “今年暑假不AC?” “是的.” “那你干什么呢?” “看世界杯呀,笨蛋!” “@#$%^&*%...” 确实如此,世界杯来了,球迷的节日也来了,估计很多ACMer也会 ...

  5. super fast sort algorithm in js

    super fast sort algorithm in js sort algorithm Promise.race (return the fast one) Async / Await // c ...

  6. greedy algorithm, insertion sort, quick sort

    always makes the choice that seems to be the best at that moment. Example #1: @function:  scheduling ...

  7. 选择排序、快速排序、归并排序、堆排序、快速排序实现及Sort()函数使用

    1.问题来源 在刷题是遇到字符串相关问题中使用 strcmp()函数. 在函数比较过程中有使用 排序函数 Sort(beg,end,comp),其中comp这一项理解不是很彻底. #include & ...

  8. 反向输出及sort排序

    建立条件:#include "algorithm"引用这个头文件 1.reverse 的用法,反向排序,由自己输入5个数: 1 2 3 4 5 for (int i = 0; i ...

  9. qsort函数、sort函数【转】

    http://blog.163.com/yuhua_kui/blog/static/9679964420142195442766/ 先说明一下:qsort和sort,只能对连续内存的数据进行排序,像链 ...

随机推荐

  1. Java 基本语法,标识符,修饰符,关键字

    基本语法 编写 Java 程序时,应注意以下几点: 大小写敏感:Java 是大小写敏感的,这就意味着标识符 Hello 与 hello 是不同的. 类名:对于所有的类来说,类名的首字母应该大写.如果类 ...

  2. spring+myBatis 配置多数据源,切换数据源

    注:本文来源于  tianzhiwuqis <spring+myBatis 配置多数据源,切换数据源> 一个项目里一般情况下只会使用到一个数据库,但有的需求是要显示其他数据库的内容,像这样 ...

  3. 在做zabbix分布式监控时遇到的各种坑

    因为公司在用zabbix的时候需要用到zabbix-proxy,所以今天就在三台虚拟机上做了测试: 环境:zabbix-server端:centos6.8 上面安装了zabbix-server.zab ...

  4. buaaoo_first_improvement

    优化,还是不优化,这是个问题 本讨论仅基于程序基本上正确的情况下. (一)第一次作业 众所周知,本次作业没有优化到100分的都进入了B组或者C组,所以事实上本次作业的优化是十分简单的,在这里提几句. ...

  5. vs2013下配置opencv1.0.0

    vs2013下配置opencv1.0.0 2019-4-25 17:13:13 原因 现在最新opencv已经到4.0.1版了.用opencv1.0原因: 小.源码包只有10M左右. 清晰.他的文档结 ...

  6. C# Conversion Keywords

    主要是解决类与其他不同数据类型的转换 类于类的显式转换: explicit  public static explicit operator B(A a) { return new B(){a.... ...

  7. HTML5原生拖拽/拖放(drag & drop)详解

    前言 拖放(drap && drop)在我们平时的工作中,经常遇到.它表示:抓取对象以后拖放到另一个位置.目前,它是HTML5标准的一部分.我从几个方面学习并实践这个功能. 拖放的流程 ...

  8. java servlet简述

    概述 servlet就相当于一个java小程序,其作用主要是为了接收并响应客户端的请求 入门 1.新建工程并新建个类 2.重写servlet的方法(重要的是service方法) 3.部署工程 a.在w ...

  9. .net core 2.x - 发送邮件

    前言 我们知道目前 .NET Core 还不支持 SMTP 协议,当我么在使用到发送邮件功能的时候,需要借助于一些第三方组件来达到目的,今天给大家介绍两款开源的邮件发送组件,它们分别是 MailKit ...

  10. Definition of matrix norms

    In my previous post, I introduced various definitions of matrix norms in \(\mathbb{R}^{n \times n}\) ...