zoukankan      html  css  js  c++  java
  • 简单排序

    简单排序分为冒泡排序、直接排序和,直接

     1 /** 
     2  * @ClassName: Sort 
     3  * @Description: 简单排序
     4  * @author dongye 
     5  * @date 2016年3月2日 上午10:26:49 
     6  *  
     7  */
     8 public class Sort {
     9     /** 
    10      * @Description: 冒泡排序
    11      *  比较相邻的元素。如果第一个比第二个大,就交换他们两个。
    12      * @author dongye  
    13      * @date 2016年3月2日 上午10:27:14 
    14      */
    15     public static void bubbleSort(long[] arr) {
    16         long tmp=0;
    17         for (int i = 0; i < arr.length-1; i++) {
    18             for (int j = arr.length-1; j > i; j--) {
    19                 if(arr[j]<arr[j-1]){
    20                     tmp=arr[j];
    21                     arr[j]=arr[j-1];
    22                     arr[j-1]=tmp;
    23                 }
    24             }
    25         }
    26     }
    27     
    28     /** 
    29      * @Description: 直接排序
    30      * 在未排序序列中找到最小元素,存放到排序序列的最后位置
    31      * @author dongye  
    32      * @date 2016年3月2日 上午18:57:14 
    33      */
    34     public static void selectionSort(long[] arr) {
    35         long tmp=0;
    36         int k=0;
    37         for (int i = 0; i < arr.length-1; i++) {
    38             k=i;
    39             for (int j = i+1; j < arr.length; j++) {
    40                 if(arr[j]<arr[k]){
    41                     k=j;
    42                 }
    43             }
    44             tmp=arr[k];
    45             arr[k]=arr[i];
    46             arr[i]=tmp;
    47             
    48         }
    49     }
    50     
    51     /** 
    52      * @Description: 直接插入排序
    53      * 先取出一个元素,和前面已经排过的对比,直到比他小停止交换,然后把这个元素放在这个位置
    54      * @author dongye  
    55      * @date 2016年3月2日 上午18:57:14 
    56      */
    57     public static void insertSort(long[] arr) {
    58         long tmp = 0;
    59         int j;
    60         for (int i = 1; i < arr.length; i++) {
    61             tmp = arr[i];
    62             j = i;
    63             while (j > 0 && arr[j-1] >= tmp) {
    64                 arr[j] = arr[j - 1];
    65                 j--;
    66             }
    67             arr[j] = tmp;
    68         }
    69     }
    70 }

    插入排序

  • 相关阅读:
    (网络编程)基于tcp(粘包问题) udp协议的套接字通信
    (网络编程)理论 原理
    (网络编程) 介绍
    C++开源代码项目汇总
    Facial Landmark Detection
    人脸对齐ASM-AAM-CLM的一些总结
    Ello讲述Haar人脸检测:易懂、很详细、值得围观
    显示形状回归算法(ESR)代码介绍
    百度图像搜索探秘
    人脸特征点检测
  • 原文地址:https://www.cnblogs.com/snow1314/p/5236251.html
Copyright © 2011-2022 走看看