• 冒泡_选择算法


    js实现冒泡,选择排序算法:

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title></title>
     6 
     7 </head>
     8 <body>
     9     <script>
    10         function bubble(arr){
    11             var temp;
    12             for(var i = 0;i<arr.length- 1;++i){
    13                 for(var j = 0;j<arr.length -i -1 ;++j){
    14                     if(arr[j]>arr[j+1]){
    15                         temp = arr[j];
    16                         arr[j]  = arr[j+1];
    17                         arr[j+1]  = temp;
    18                     }
    19                 }
    20             }
    21             return arr;
    22         }
    23         function select(arr){
    24             var min_idx;
    25             var temp;
    26             for(var i =0 ;i<arr.length-1;++i){
    27                 min_idx = i;
    28                 for(var j = i+1;j<arr.length;++j){
    29                     if(arr[min_idx] > arr[j]){
    30                         min_idx = j;
    31                     }
    32                 }
    33                 if(min_idx != i){
    34                     temp = arr[min_idx];
    35                     arr[min_idx] = arr[i];
    36                     arr[i] = temp;
    37                 }
    38             }
    39             return arr;
    40         }
    41 
    42         var arr=  [1,23,55,6,7,8,32,32,34,53,34,3,12,321,53,434,43,5,56,6,7,4,54];
    43         //冒泡
    44         var ret = bubble(arr);
    45 
    46         //选择
    47         // var ret = select(arr);
    48         console.log(ret);
    49     </script>
    50 
    51 </body>
    52 </html>
    View Code

    Python 实现冒泡,选择排序算法:

     1 def bubble(arr):
     2     for i in range(len(arr) - 1):
     3         for j in range(len(arr) - 1 -i):
     4             if arr[j] >arr[j+1]:
     5                 temp = arr[j]
     6                 arr[j] = arr[j+1]
     7                 arr[j+1] = temp
     8     return arr
     9 def select(arr):
    10     # min_idx
    11     # temp
    12     for i in range(len(arr)-1):
    13         min_idx = i
    14         for j in range(i+1, len(arr)):
    15             if arr[min_idx] > arr[j]:
    16                 min_idx = j
    17         if min_idx != i:
    18             temp = arr[min_idx]
    19             arr[min_idx] = arr[i]
    20             arr[i] = temp
    21     return arr
    22 
    23 if __name__ == '__main__':
    24     import time
    25     arr = [1, 23, 1, 53, 434, 43, 5, 56, 6, 7, 4, 54, 6, 7, 8, 32, 32, 34,
    26            53, 34, 3, 12, 321, 53, 434, 43, 5, 56]
    27 
    28     #冒泡排序
    29     t1 = time.time()
    30     ret = bubble(arr)
    31     time.sleep(1)
    32     print(ret,"用时:{}".format(time.time()-t1))
    33 
    34     #选择排序
    35     t2 = time.time()
    36     ret = select(arr)
    37     time.sleep(1)
    38     print(ret,"用时:{}".format(time.time()-t2))
    39     '''
    40     输出:
    41     [1, 1, 3, 4, 5, 5, 6, 6, 7, 7, 8, 12, 23, 32, 32, 34, 34, 43, 43, 53, 53, 53, 54, 56, 56, 321, 434, 434] 用时:1.0002381801605225
    42     [1, 1, 3, 4, 5, 5, 6, 6, 7, 7, 8, 12, 23, 32, 32, 34, 34, 43, 43, 53, 53, 53, 54, 56, 56, 321, 434, 434] 用时:1.0001587867736816
    43     '''
    View Code

    Java 实现冒泡,选择排序算法:

    pass

    C/C++ 实现冒泡,选择排序算法:

     1 #include <stdio.h>
     2 int*  bubbleSort(int* arr,int len){
     3     for (int i=0;i<len -1;i++){
     4         for (int j=0;j<len -i-1;j++){
     5             if(arr[j] > arr[j+1]){
     6                 arr[j] = arr[j] ^ arr[j+1];
     7                 arr[j+1] = arr[j]^ arr[j+1];
     8                 arr[j] = arr[j] ^arr[j+1];
     9             }
    10 
    11         }
    12     }
    13     return arr;
    14 }
    15 int main(){
    16     int a[] = {1,2,3,4,5,18,2,3,4,5,10,32,1,2,3};
    17     int len = sizeof(a)/ sizeof(a[0]);
    18 
    19     int* arr = bubbleSort(a,len);
    20 
    21     for (int i = 0; i < len; ++i) {
    22         printf("arr: %d
    ",arr[i]);
    23     }
    24 }
    BubbleSort() c语言
     1 #include <stdio.h>
     2 int*  chooseSort(int* arr,int len){
     3     for (int i=0;i<len;++i){
     4         int idx = i;
     5         for (int j = i+1; j < len; ++j) {
     6             if (arr[idx] > arr[j]){
     7                 idx = j;  // 记录下 索引!
     8             }
     9         }
    10         if(idx != i){
    11             arr[idx] = arr[idx] ^ arr[i];
    12             arr[i] = arr[idx]  ^ arr[i];
    13             arr[idx] = arr[idx]  ^ arr[i];
    14         }
    15     }
    16 
    17     return arr;
    18 }
    19 int main(){
    20     int a[] = {1,2,3,4,5,18,2,3,4,5,10,32,1,2,3};
    21     int len = sizeof(a)/ sizeof(a[0]);
    22 
    23     int* arr = chooseSort(a,len);
    24 
    25     for (int i = 0; i < len; ++i) {
    26         printf("arr: %d
    ",arr[i]);
    27     }
    28 }
    chooseSort() c语言
  • 相关阅读:
    SiteMesh在项目中的配置
    javascript深入理解js闭包
    小tip: 使用CSS将图片转换成黑白(灰色、置灰)
    java程序员最不愿意看到的十件事
    Spring AOP AspectJ
    2万字Java并发编程面试题整理(含答案,建议收藏)
    85道Java微服务面试题整理(助力2020面试)
    10个很多人不知道的Redis使用技巧
    2020年大厂Java面试题(基础+框架+系统架构+分布式+实战)
    2020年薪30W的Java程序员都要求熟悉JVM与性能调优!
  • 原文地址:https://www.cnblogs.com/zach0812/p/11592221.html
Copyright © 2020-2023  润新知