js的几种排序

转载:http://www.jb51.net/article/81520.htm

一.冒泡排序

?


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

function BubbleSort(array) {

  var length = array.length;

  for (var i = length - 1; i > 0; i--) { //用于缩小范围

    for (var j = 0; j < i; j++) { //在范围内进行冒泡,在此范围内最大的一个将冒到最后面

      if (array[j] > array[j+1]) {

        var temp = array[j];

        array[j] = array[j+1];

        array[j+1] = temp;

      }

    }

    console.log(array);

    console.log("-----------------------------");

  }

  return array;

}

var arr = [10,9,8,7,7,6,5,11,3];

var result = BubbleSort(arr);

console.log(result);

/*

[ 9, 8, 7, 7, 6, 5, 10, 3, 11 ]

-----------------------------

[ 8, 7, 7, 6, 5, 9, 3, 10, 11 ]

-----------------------------

[ 7, 7, 6, 5, 8, 3, 9, 10, 11 ]

-----------------------------

[ 7, 6, 5, 7, 3, 8, 9, 10, 11 ]

-----------------------------

[ 6, 5, 7, 3, 7, 8, 9, 10, 11 ]

-----------------------------

[ 5, 6, 3, 7, 7, 8, 9, 10, 11 ]

-----------------------------

[ 5, 3, 6, 7, 7, 8, 9, 10, 11 ]

-----------------------------

[ 3, 5, 6, 7, 7, 8, 9, 10, 11 ]

-----------------------------

[ 3, 5, 6, 7, 7, 8, 9, 10, 11 ]

*/

二.选择排序

?


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

function SelectionSort(array) {

  var length = array.length;

  for (var i = 0; i < length; i++) { //缩小选择的范围

    var min = array[i]; //假定范围内第一个为最小值

    var index = i; //记录最小值的下标

    for (var j = i + 1; j < length; j++) { //在范围内选取最小值

      if (array[j] < min) {

        min = array[j];

        index = j;

      }

    }

    if (index != i) { //把范围内最小值交换到范围内第一个

      var temp = array[i];

      array[i] = array[index];

      array[index] = temp;

    }

    console.log(array);

    console.log("---------------------");

  }

  return array;

}

var arr = [ 1, 10, 100, 90, 65, 5, 4, 10, 2, 4 ];

var result = SelectionSort(arr);

console.log(result);

/*

[ 1, 10, 100, 90, 65, 5, 4, 10, 2, 4 ]

---------------------

[ 1, 2, 100, 90, 65, 5, 4, 10, 10, 4 ]

---------------------

[ 1, 2, 4, 90, 65, 5, 100, 10, 10, 4 ]

---------------------

[ 1, 2, 4, 4, 65, 5, 100, 10, 10, 90 ]

---------------------

[ 1, 2, 4, 4, 5, 65, 100, 10, 10, 90 ]

---------------------

[ 1, 2, 4, 4, 5, 10, 100, 65, 10, 90 ]

---------------------

[ 1, 2, 4, 4, 5, 10, 10, 65, 100, 90 ]

---------------------

[ 1, 2, 4, 4, 5, 10, 10, 65, 100, 90 ]

---------------------

[ 1, 2, 4, 4, 5, 10, 10, 65, 90, 100 ]

---------------------

[ 1, 2, 4, 4, 5, 10, 10, 65, 90, 100 ]

---------------------

[ 1, 2, 4, 4, 5, 10, 10, 65, 90, 100 ]

*/

三.插入排序

?


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

function InsertionSort(array) {

  var length = array.length;

  for (var i = 0; i < length - 1; i++) {

    //i代表已经排序好的序列最后一项下标

    var insert = array[i+1];

    var index = i + 1;//记录要被插入的下标

    for (var j = i; j >= 0; j--) {

      if (insert < array[j]) {

        //要插入的项比它小,往后移动

        array[j+1] = array[j];

        index = j;

      }

    }

    array[index] = insert;

    console.log(array);

    console.log("-----------------------");

  }

  return array;

}

var arr = [100,90,80,62,80,8,1,2,39];

var result = InsertionSort(arr);

console.log(result);

/*

[ 90, 100, 80, 62, 80, 8, 1, 2, 39 ]

-----------------------

[ 80, 90, 100, 62, 80, 8, 1, 2, 39 ]

-----------------------

[ 62, 80, 90, 100, 80, 8, 1, 2, 39 ]

-----------------------

[ 62, 80, 80, 90, 100, 8, 1, 2, 39 ]

-----------------------

[ 8, 62, 80, 80, 90, 100, 1, 2, 39 ]

-----------------------

[ 1, 8, 62, 80, 80, 90, 100, 2, 39 ]

-----------------------

[ 1, 2, 8, 62, 80, 80, 90, 100, 39 ]

-----------------------

[ 1, 2, 8, 39, 62, 80, 80, 90, 100 ]

-----------------------

[ 1, 2, 8, 39, 62, 80, 80, 90, 100 ]

*/

四.希尔排序

?


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

function ShellSort(array) {

  var length = array.length;

  var gap = Math.round(length / 2);

  while (gap > 0) {

    for (var i = gap; i < length; i++) {

      var insert = array[i];

      var index = i;

      for (var j = i; j >= 0; j-=gap) {

        if (insert < array[j]) {

          array[j+gap] = array[j];

          index = j;

        }

      }

      array[index] = insert;

    }

    console.log(array);

    console.log("-----------------------");

    gap = Math.round(gap/2 - 0.1);

  }

  return array;

}

var arr = [ 13, 14, 94, 33, 82, 25, 59, 94, 65, 23, 45, 27, 73, 25, 39, 10 ];

var result = ShellSort(arr);

console.log(result);

/*

[ 13, 14, 45, 27, 73, 25, 39, 10, 65, 23, 94, 33, 82, 25, 59, 94 ]

-----------------------

[ 13, 14, 39, 10, 65, 23, 45, 27, 73, 25, 59, 33, 82, 25, 94, 94 ]

-----------------------

[ 13, 10, 39, 14, 45, 23, 59, 25, 65, 25, 73, 27, 82, 33, 94, 94 ]

-----------------------

[ 10, 13, 14, 23, 25, 25, 27, 33, 39, 45, 59, 65, 73, 82, 94, 94 ]

-----------------------

[ 10, 13, 14, 23, 25, 25, 27, 33, 39, 45, 59, 65, 73, 82, 94, 94 ]

*/

五.归并排序

?


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

function MergeSort(array) {

  var length = array.length;

  if (length <= 1) {

    return array;

  } else {

    var num = Math.ceil(length/2);

    var left = MergeSort(array.slice(0, num));

    var right = MergeSort(array.slice(num, length));

    return merge(left, right);

  }

}

function merge(left, right) {

  console.log(left);

  console.log(right);

  var a = new Array();

  while (left.length > 0 && right.length > 0) {

    if (left[0] <= right[0]) {

      var temp = left.shift();

      a.push(temp);

    } else {

      var temp = right.shift();

      a.push(temp);

    }

  }

  if (left.length > 0) {

    a = a.concat(left);

  }

  if (right.length > 0) {

    a = a.concat(right);

  }

  console.log(a);

  console.log("-----------------------------");

  return a;

}

var arr = [ 13, 14, 94, 33, 82, 25, 59, 94, 65, 23, 45, 27, 73, 25, 39, 10 ];

var result = MergeSort(arr);

console.log(result);

/*

[ 13 ]

[ 14 ]

[ 13, 14 ]

-----------------------------

[ 94 ]

[ 33 ]

[ 33, 94 ]

-----------------------------

[ 13, 14 ]

[ 33, 94 ]

[ 13, 14, 33, 94 ]

-----------------------------

[ 82 ]

[ 25 ]

[ 25, 82 ]

-----------------------------

[ 59 ]

[ 94 ]

[ 59, 94 ]

-----------------------------

[ 25, 82 ]

[ 59, 94 ]

[ 25, 59, 82, 94 ]

-----------------------------

[ 13, 14, 33, 94 ]

[ 25, 59, 82, 94 ]

[ 13, 14, 25, 33, 59, 82, 94, 94 ]

-----------------------------

[ 65 ]

[ 23 ]

[ 23, 65 ]

-----------------------------

[ 45 ]

[ 27 ]

[ 27, 45 ]

-----------------------------

[ 23, 65 ]

[ 27, 45 ]

[ 23, 27, 45, 65 ]

-----------------------------

[ 73 ]

[ 25 ]

[ 25, 73 ]

-----------------------------

[ 39 ]

[ 10 ]

[ 10, 39 ]

-----------------------------

[ 25, 73 ]

[ 10, 39 ]

[ 10, 25, 39, 73 ]

-----------------------------

[ 23, 27, 45, 65 ]

[ 10, 25, 39, 73 ]

[ 10, 23, 25, 27, 39, 45, 65, 73 ]

-----------------------------

[ 13, 14, 25, 33, 59, 82, 94, 94 ]

[ 10, 23, 25, 27, 39, 45, 65, 73 ]

[ 10, 13, 14, 23, 25, 25, 27, 33, 39, 45, 59, 65, 73, 82, 94, 94 ]

-----------------------------

[ 10, 13, 14, 23, 25, 25, 27, 33, 39, 45, 59, 65, 73, 82, 94, 94 ]

*/

六.快速排序

?


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

function QuickSort(array) {

  var length = array.length;

  if (length <= 1) {

    return array;

  } else {

    var smaller = [];

    var bigger = [];

    var base = [array[0]];

    for (var i = 1; i < length; i++) {

      if (array[i] <= base[0]) {

        smaller.push(array[i]);

      } else {

        bigger.push(array[i]);

      }

    }

    console.log(smaller.concat(base.concat(bigger)));

    console.log("-----------------------");

    return QuickSort(smaller).concat(base.concat(QuickSort(bigger)));

  }

}

var arr = [ 8, 10, 100, 90, 65, 5, 4, 10, 2, 4 ];

var result = QuickSort(arr);

console.log(result);

/*

[ 5, 4, 2, 4, 8, 10, 100, 90, 65, 10 ]

-----------------------

[ 4, 2, 4, 5 ]

-----------------------

[ 2, 4, 4 ]

-----------------------

[ 2, 4 ]

-----------------------

[ 10, 10, 100, 90, 65 ]

-----------------------

[ 90, 65, 100 ]

-----------------------

[ 65, 90 ]

-----------------------

[ 2, 4, 4, 5, 8, 10, 10, 65, 90, 100 ]

*/

http://www.cnblogs.com/dushao/p/6004883.html

时间: 2024-10-13 23:00:07

js的几种排序的相关文章

9种排序

一:你必须知道的 1> JS原型 2> 排序中的有序区和无序区 3> 二叉树的基本知识 如果你不知道上面三个东西,还是去复习一下吧,否则,看下面的东西有点吃力. 二:封装丑陋的原型方法 Function.prototype.method = function(name, func){ this.prototype[name] = func; return this; }; 在下面的排序中都要用到这个方法. 三:9种排序算法的思路和实现 1> 插入排序 基本思路: 从无序区的第一个元

八种排序算法

最近一段时间自己在研究各种排序算法,于是自己写了一个八种排序算法的集合: /************************************************************************* > Copyright (c)2014 stay hungry,stay foolish !!! > File Name: sort.cpp > Author: kanty > Mail: [email protected] > Created Time:

sort_contours_xld算子的几种排序方式研究

sort_contours_xld算子有5种排序方式,即: 'upper_left': The position is determined by the upper left corner of the surrounding rectangle. 'upper_right':The position is determined by the upper right corner of the surrounding rectangle. 'lower_left':The position i

模板化的七种排序算法,适用于T* vector&lt;T&gt;以及list&lt;T&gt;

最近在写一些数据结构以及算法相关的代码,比如常用排序算法以及具有启发能力的智能算法.为了能够让写下的代码下次还能够被复用,直接将代码编写成类模板成员函数的方式,之所以没有将这种方式改成更方便的函数模板纯属于偷懒,更方便于测试代码的有效性,等代码写完也懒得去改了.下面开始介绍这段代码,有什么不对的地方欢迎前来指正. 一共写了七种排序,插入排序InsertSort.堆排序HeapSort.快速排序QuickSort.合并排序MergeSort,计数排序CountingSort,基数排序RadixSo

js的6种继承方式

重新理解js的6种继承方式 注:本文引用于http://www.cnblogs.com/ayqy/p/4471638.html 重点看第三点 组合继承(最常用) 写在前面 一直不喜欢JS的OOP,在学习阶段好像也用不到,总觉得JS的OOP不伦不类的,可能是因为先接触了Java,所以对JS的OO部分有些抵触. 偏见归偏见,既然面试官问到了JS的OOP,那么说明这东西肯定是有用的,应该抛开偏见,认真地了解一下 约定 P.S.下面将展开一个有点长的故事,所以有必要提前约定共同语言: 1 2 3 4 5

10种排序算法分析

10种排序算法,分别是直接插入排序,折半插入排序,希尔排序,冒泡排序,快速排序,直接选择排序,树形排序,堆排序,归并排序,基数排序.各有千秋,但依旧有优劣之分,熟悉每一个算法,对于我们的代码优化,也将事半功倍. 1,直接插入排序: 基本思想: 假设待排的n个记录存放在变量R中,首先将R[1]看做是有序区,将后n - 1个数组元素看作是无序区:然后将无序区的第一个元素R[2]插入到前面有序区的适当位置,从而得到新的有序区R[1..2]:依次类推,经过n - 1趟直接插入排序后,得到有序区R[1..

几种排序算法比较

图解数据结构(10)——排序 十四.排序(Sort) http://www.cppblog.com/guogangj/archive/2009/11/13/100876.html <Thinking in Algorithm>12.详解十一种排序算法 http://blog.csdn.net/speedme/article/details/23021467

整理的8种排序算法的总结和比较

1 快速排序(QuickSort) 快速排序是一个就地排序,分而治之,大规模递归的算法.从本质上来说,它是归并排序的就地版本.快速排序可以由下面四步组成. (1) 如果不多于1个数据,直接返回. (2) 一般选择序列最左边的值作为支点数据. (3) 将序列分成2部分,一部分都大于支点数据,另外一部分都小于支点数据. (4) 对两边利用递归排序数列. 快速排序比大部分排序算法都要快.尽管我们可以在某些特殊的情况下写出比快速排序快的算法,但是就通常情况而言,没有比它更快的了.快速排序是递归的,对于内

HashMap的两种排序方式

Map<String, Integer> map = new HashMap<String, Integer>(); map.put("d", 2); map.put("c", 1); map.put("b", 1); map.put("a", 3); List<Map.Entry<String, Integer>> infoIds = new ArrayList<Map.