heyheyhey ~~

It has been a long time since i come here again...whatever today i will summerize some methods of sort with java what are so important for coder. The codes below are all compiled successfully and have the right results

. insert sort -- is divided into insert directly and Shell Sort.

1. insert directly -- the main idea is while obtaining a new value, to compare the new value with the number of sorted array, when get the position that the value is lager than the front number and smaller than the behind number,  insert the value.

public class InsertDirectly {

  public static void insertDirectly(int[] a) {

    for(int i = 1; i<a.length; ++i) {

      // because maybe the insert value would be complicanted , so we should define a copy for it

      int temp = a[i];

      // then go through the every number of the sorted array to find out the right position.

      // to make the j be the global variable.

      int j = i-1

      // because maybe the a[i] would be complicanted,  so a[j] shouled > temp,not a[i]

      for(; j >= 0 && a[j] > temp; --j) {

        a[j+1] = a[j]; 

      }

      a[j+1] = temp;

    }

  }

}

2. Shell Sort is the special sort of insert sort, just because it has the increment-'d' ,not one ny one any more.

main idea is just according to the 'd' to carry out the method of directly insert sort

public class ShellSort{

  public static void shellSort(int[] a) {

    // firt step :need to init the 'd'.

    double d1 = a.length;

    // the 'd' need to be changed, so make sure the loop

    while(true) {

      //ceil -- get the bigger one

      double d1 = Math.ceil(d1/2);

      int d = (int) d1;

      // for the outer loop

      for(int i = 0; i<d; ++i) {

        //for the increment loop

        for(int j = i+d; j < a.length; i+=d) {

          int temp = a[j];

          // for the inner loop

          int x = j-d;

          for(; x >= 0 && a[x] > temp; x-=d) {

            a[x+d] = a[x];

          }

          a[x+d] = temp;

        }

      }

      if( d == 1) break;

    }

  }

}

二. select sort -- is divided into easy select sort and heap sort.

1. easy select sort is the most easy sort --  main idea is make sure the loop of find out the lagest one or the smallest one ,and put it to the rear or head of the array

public class EasySort {

  public static void swap(int[] a ,int x, int y) {

    int temp = a[x];

    a[x] = a[y];

    a[y] = temp;

  }

  public static void easySort(int[] a) {

    for(int i = 0; i<a.length; ++i) {

      int max = a[i];

      int flag = i;

      for(int j = i+1; j < a.length;++j) {

        if(max < a[j]) {

          max = a[j];

          flag = j;

        }

      }

      swap(a, a.length-1-i, flag);

    }

  }

}

2.Heap Sort is actually the selected sort based on heap -- main idea is that build max or min heap and then change the position bettween the max or min and the last position.

public class HeapSort {

  // this methid is mainly to change the max value which is in the top of heap(in other word which is always the first position of the array)

// with the last number of this array.

  public static void swap(int[] a, int i, int j) {

    int temp = a[i];

    a[i] = a[j];

    a[j] = temp;

  }

  public static void heapSort(int[] a) {

    // from the last value of the array in order to buildMaxHeap circularly.

    for(int i = a.length-1; i > 0; --i) {

      buildMaxHeap(a, i);

      swap(a, 0, i);

    }

  }

  // next is the highlight of the heap sort ,and in this method we would to let u know how to bulid the max heap and how to reform the heap a

// again and again.

   public static void buildMaxHeap(int[] a,int lastIndex) {

    // step 1 : get the last father node of this heap

    int lastFather = (lastIndex-1)/2;

    // step 2 : we need to find out the max value among the father node and the subnodes

    for(int i = lastFather; i >= 0; --i) {

      // step 2.1 : first we need to find out the max value bettween the subnodes

      int biggerIndex = lastFather*2+1; //  make the left node be the max first and then judge if the right node exists

      if(biggerIndex < lastIndex) // make sure the right node exists {

        if(a[biggerIndex] < a[lastIndex]) {

          biggerIndex ++;

        }

      }

      // step 2.2 :  second we need to compare the biggerOne and the father node

      if(a[lastFather] < a[biggerIndex]) {

        // let the larger value go to the top

        swap(a, lastFather, biggerIndex);

      }

    }

  }

}

Sort Methods的更多相关文章

  1. reverse(), extend(), sort() methods of list

    >>> l = list('sdf') >>> l ['s', 'd', 'f'] >>> id(l) 4520422000 >>&g ...

  2. 总结: Sort 排序算法

    排序总结 面试经验 硅谷某前沿小Startup面试时,问到的一个题目就是写一个快速排序算法.进而面试官问到了各种算法的算法复杂度,进而又问了Merge Sort 与 QuickSort 的优劣. 对排 ...

  3. 谈谈我对前端组件化中“组件”的理解,顺带写个Vue与React的demo

    前言 前端已经过了单兵作战的时代了,现在一个稍微复杂一点的项目都需要几个人协同开发,一个战略级别的APP的话分工会更细,比如携程: 携程app = 机票频道 + 酒店频道 + 旅游频道 + ..... ...

  4. MacOS10.11的/usr/bin目录不可写后class-dump的处理办法

    许多升级了OSX 10.11的朋友在配置class-dump的时候,会发现书上推荐的class-dump存放目录/usr/bin不再可写,如下所示: 192:~ snakeninny$ touch c ...

  5. Core Java Volume I — 3.10. Arrays

    3.10. ArraysAn array is a data structure that stores a collection of values of the same type. You ac ...

  6. go实例之排序

    1.默认排序 使用sort包进行排序.排序是就地排序,因此它会更改给定的切片,并且不返回新的切片. package main import "fmt" import "s ...

  7. mina statemachine解读(一)

      statemachine(状态机)在维护多状态数据时有非常好的作用,现在github上star排名最前的是squirrel-foundation以及spring-statemachine,而min ...

  8. Comparable vs Comparator

    Comparable interface can be used to provide single way of sorting whereas Comparator interface is us ...

  9. class-dump 使用

    转:class-dump 使用 class-dump 官网地址:这里 我这里下载的是 class-dump-3.5.dmg 版本的.双击.dmg 文件,将  拉倒 /usr / local / bin ...

随机推荐

  1. WinForm下增加声音提示

    在WinForm平台下播放声音,一般有两种方式:第一种是调用系统自带声音: [代码] 申明定义: [DllImport("kernel32.dll")] public static ...

  2. Android ActionBar 初探

    1.指南,例子,个人感觉 首先上官网指南链接http://developer.android.com/guide/topics/ui/actionbar.html 参考了官网上的例子http://de ...

  3. 无意之间发现的Servlet3.0新特性@WebServlet

    今天无意之间看到了一个注解,这个注解就是@WebServlet,@WebServlet 用于将一个类声明为 Servlet,该注解将会在部署时被容器处理,容器将根据具体的属性配置将相应的类部署为 Se ...

  4. Google的Protobuf协议分析

    protobuf和thrift类似,也是一个序列化的协议实现,简称PB(下文出现的PB代表protobuf). Github:https://github.com/google/protobuf 上图 ...

  5. 转:入门Webpack,看这篇就够了

    写在前面的话 阅读本文之前,先看下面这个webpack的配置文件,如果每一项你都懂,那本文能带给你的收获也许就比较有限,你可以快速浏览或直接跳过:如果你和十天前的我一样,对很多选项存在着疑惑,那花一段 ...

  6. C和指针 第十六章 标准函数库 信号

    信号名<signal.h> 程序中大多数错误都是程序本身导致的,但是,有些程序遇到的事件却不是程序本身所引发的.比如用户终止程序,程序无法预知此类事件发生的情况,信号就是为了对此类事件做出 ...

  7. Spring4读书笔记(1)-模块

    Srping主要模块 Core Container spring-core,spring-beans: 提供基础功能,包括IoC和DI等特性.对依赖起到解耦作用(BeanFactory). sprin ...

  8. [BZOJ1861][Zjoi2006]Book 书架

    [BZOJ1861][Zjoi2006]Book 书架 试题描述 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书的时候 ...

  9. jQuery判断及更改checkbox状态

    判断:jquery对象.prop("checked") 选中:jquery对象.prop("checked", true) 取消选中:jquery对象.remo ...

  10. .NET+IIS+MSSQL配置

    好久没配置.NET+IIS+MSSQL了,跟以前不大一样了.总结下吧. 环境: Windows Server 2012 标准版 x64 SQL Server Express 2014 一.HTTP E ...