A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 我是小水水 初级黑马   /  2015-5-26 11:00  /  6349 人查看  /  31 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

18黑马币
排序的一些方法,需要代码!

最佳答案

查看完整内容

刚发的不好。。重新发一个,差分啊,版主给分哇。。。 1选择排序 * int[] arr = {66,55,44,33,22,11}; 原理:如果拿0角标上的元素依次和后面的元素进行比较, 第一次内循环结束后,最小值出现在了0角标位置。 arr[0]与arr[1-5]比了五次 arr[1]与arr[2-5]比了四次 arr[2]与arr[3-5]比了三次 arr[3]与arr[4-5]比了二次 arr[4]与arr[5]比了一次 你就想想我们是如何打星星 ***** **** ** ...

31 个回复

倒序浏览
刚发的不好。。重新发一个,差分啊,版主给分哇。。。
1选择排序
*
                int[] arr = {66,55,44,33,22,11};
                原理:如果拿0角标上的元素依次和后面的元素进行比较,
                      第一次内循环结束后,最小值出现在了0角标位置。
                arr[0]与arr[1-5]比了五次
                arr[1]与arr[2-5]比了四次
                arr[2]与arr[3-5]比了三次
                arr[3]与arr[4-5]比了二次
                arr[4]与arr[5]比了一次
                你就想想我们是如何打星星
                *****
                ****
                ***
                **
                *
                arr[x]与arr[y]比较
                数组长度是6
                for (int x = 0;x < arr.length - 1;x++){
                        for (int y = x + 1;y < arr.length;y++){
                                if (arr[x] > arr[y]){
                                        int temp = arr[x];
                                        arr[x] = arr[y];
                                        arr[y] = temp;
                                }
                        }
                }
               
* 2冒泡排序

                int[] arr = {66,55,44,33,22,11};
                原理:两个相邻元素进行比较,第一次比较完以后,最大值出现在了最大角标处。
                第一次:arr[0]与arr[1],arr[1]与arr[2],arr[2]与arr[3],arr[3]与arr[4],arr[4]与arr[5],比了五次
                第二次:arr[0]与arr[1],arr[1]与arr[2],arr[2]与arr[3],arr[3]与arr[4]比了四次
                第三次:arr[0]与arr[1],arr[1]与arr[2],arr[2]与arr[3]比了三次
                第四次:arr[0]与arr[1],arr[1]与arr[2]比了二次
                第五次:arr[0]与arr[1]比了一次
                for (int x = 0;x < arr.length - 1; x++){
                        //-1防止角标越界
                        //-x为了提高效率
                        for (int y = 0;y < arr.length - 1 - x;y++){//6
                                if (arr[y] > arr[y+1]){
                                        int temp = arr[y];
                                        arr[y] = arr[y+1];
                                        arr[y+1] = temp;
                                }
                        }
                }
* 3,查找
        * A:无序数组
        *
               
                        int[] arr = {33,22,11,44,55,66};
                        public static int getIndex(int[] arr,int key) {
                                for (int x = 0;x < arr.length;x++){
                                        if (key == arr[x]){
                                                return x;
                                        }
                                }
                                return -1;
                        }
        * B:有序数组 二分查找
        *
                        数组长度是6,最大角标值是5
                        public static int getIndex(int[] arr,int key) {
                                int min = 0;
                                int max = arr.length-1;
                                int mid = (min + max)/2;
                                while (key != arr[mid]){
                                        if (key > arr[mid]){
                                                min = mid + 1;
                                        }else if (key < arr[mid]){
                                                max = mid - 1;
                                        }
                                        if (min > max){
                                                return -1;
                                        }
                                        mid = (min + max)/2;
                                }
                                return mid;
                               
                        }
回复 使用道具 举报
这篇博客里面讲的很详细了
http://blog.csdn.net/hguisu/article/details/7776068
回复 使用道具 举报

这篇博客里面讲的很详细了
http://blog.csdn.net/hguisu/article/details/7776068
回复 使用道具 举报
//选择排序
public static void selectSort(int[] arr)
        {
                for (int x=0;x<arr.length-1 ; x++)//当剩下最后一位的时候不需要比较
                {
                        for (int y=x+1;y<arr.length ; y++)//每次开始比较的数y都比x大一位
                        {
                                if (arr[x]>arr[y])//条件满足大数后移
                                {
                                        swap(arr,x,y);
                                }
                        }
                }
        }
//冒泡排序
public static void bubbleSort(int[] arr)
        {
                for (int x=0;x<arr.length-1 ;x++ )//最后一位数就不许要在比较
                {
                        for (int y=0;y<arr.length-x-1 ;y++ )//相邻比较,每次循环后最后一位确定,每次少循环一次因if条件是y和y+1所以为满足条件还需减1
                        {
                                if (arr[y]>arr[y+1])
                                {
                                        swap(arr,y,y+1);
                                }
                        }
                }
        }
//定义一个变量调换函数
private static void swap(int[] arr,int a,int b)
        {
                int temp=arr[a];
                        arr[a]=arr[b];
                        arr[b]=temp;
        }
一般经常用到的就选择排序和冒泡排序,就两个关键点,代码差别不是很大,多敲几遍就记住了!
回复 使用道具 举报
/**      * 直接选择排序      */     private static void directChooseSort ( int[] array )     {         for ( int i = 0; i < array.length; i++ )         {             int index = i;             for ( int j = i + 1; j < array.length; j++ )             {                 if (array[index] > array[j])                 {                     index = j;                 }             }             if (i != index)             {                 int temp = array[i];                 array[i] = array[index];                 array[index] = temp;             }         }     }       /**      * 堆排序      */     private static void heapSort ( int[] array, int start, int len )     {         int pos = ( len - 1 ) / 2;         for ( int i = pos; i >= 0; i-- )         {             int tmp = array[start + i];             int index = i * 2 + 1;             while (index < len)             {                 if (index + 1 < len && array[start + index] < array[start + index + 1]) // 从小到大                 {                     index += 1;                 }                 if (tmp < array[start + index]) // 从小到大                 {                     array[start + i] = array[start + index];                     i = index;                     index = i * 2 + 1;                 }                 else                 {                     break;                 }             }             array[start + i] = tmp;         }         for ( int i = 0; i < len; i++ )         {             int temp = array[start];             array[start] = array[start + len - 1 - i];             array[start + len - 1 - i] = temp;             // 再一次             int post = 0;             int tmp = array[start + post];             int index = 2 * post + 1;             while (index < len - 1 - i)             {                 if (index + 1 < len - 1 - i && array[start + index] < array[start + index + 1]) // 从小到大                 {                     index += 1;                 }                 if (tmp < array[start + index]) // 从小到大                 {                     array[start + post] = array[start + index];                     post = index;                     index = post * 2 + 1;                 }                 else                 {                     break;                 }             }             array[start + post] = tmp;         }     }       /**      * 冒泡排序      */     private static void bubbleSort ( int[] array )     {         for ( int i = 0; i < array.length; i++ )         {             for ( int j = i + 1; j < array.length; j++ )             {                 if (array[i] > array[j])                 {                     int temp = array[i];                     array[i] = array[j];                     array[j] = temp;                 }             }         }     }       /**      * 快速排序      */     private static void quickSort ( int[] array, int start, int end )     {         if (start < end)         {             int key = array[start];             int i = start;             for ( int j = start + 1; j < end + 1; j++ )             {                 if (key > array[j])                 {                     int temp = array[j];                     array[j] = array[i + 1];                     array[i + 1] = temp;                     i++;                 }             }             array[start] = array[i];             array[i] = key;             quickSort (array, start, i - 1);             quickSort (array, i + 1, end);         }     }
回复 使用道具 举报
(1)“冒泡法” 冒泡法大家都较熟悉。其原理为从a[0]开始,依次将其和后面的元素比较,若a[0]>a[i],则交换它们,一直比较到a[n]。同理对a[1],a[2],...a[n-1]处理,即完成排序。下面列出其代码:搜索void bubble(int *a,int n) /*定义两个参数:数组首地址与数组大小*/ { int i,j,temp; for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) /*注意循环的上下限*/ if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } 冒泡法原理简单,但其缺点是交换次数多,效率低。 下面介绍一种源自冒泡法但更有效率的方法“选择法”。 (2)“选择法” 选择法循环过程与冒泡法一致,它还定义了记号k=i,然后依次把a[k]同后面元素比较,若a[k]>a[j],则使k=j.最后看看k=i是否还成立,不成立则交换a[k],a[i],这样就比冒泡法省下许多无用的交换,提高了效率。void choise(int *a,int n) { int i,j,k,temp; for(i=0;i<n-1;i++) { k=i; /*给记号赋值*/ for(j=i+1;j<n;j++) if(a[k]>a[j]) k=j; /*是k总是指向最小元素*/ if(i!=k) { /*当k!=i是才交换,否则a[i]即为最小*/ temp=a[i]; a[i]=a[k]; a[k]=temp; } } } 选择法比冒泡法效率更高,但说到高效率,非“快速法”莫属,现在就让我们来了解它。 (3)“快速法” 快速法定义了三个参数,(数组首地址*a,要排序数组起始元素下标i,要排序数组结束元素下标j). 它首先选一个数组元素(一般为a[(i+j)/2],即中间元素)作为参照,把比它小的元素放到它的左边,比它大的放在右边。然后运用递归,在将它左,右两个子数组排序,最后完成整个数组的排序。下面分析其代码:void quick(int *a,int i,int j) { int m,n,temp; int k; m=i; n=j; k=a[(i+j)/2]; /*选取的参照*/ do { while(a[m]<k&&m<j) m++; /* 从左到右找比k大的元素*/ while(a[n]>k&&n>i) n--; /* 从右到左找比k小的元素*/ if(m<=n) { /*若找到且满足条件,则交换*/ temp=a[m]; a[m]=a[n]; a[n]=temp; m++; n--; } }while(m<=n); if(m<j) quick(a,m,j); /*运用递归*/ if(n>i) quick(a,i,n); } (4)“插入法” 插入法是一种比较直观的排序方法。它首先把数组头两个元素排好序,再依次把后面的元素插入适当的位置。把数组元素插完也就完成了排序。void insert(int *a,int n) { int i,j,temp; for(i=1;i<n;i++) { temp=a[i]; /*temp为要插入的元素*/ j=i-1; while(j>=0&&temp<a[j]) { /*从a[i-1]开始找比a[i]小的数,同时把数组元素向后移*/ a[j+1]=a[j]; j--; } a[j+1]=temp; /*插入*/ } } (5)“shell法” shell法是一个叫 shell 的美国人与1969年发明的。它首先把相距k(k>=1)的那几个元素排好序,再缩小k值(一般取其一半),再排序,直到k=1时完成排序。下面让我们来分析其代码:void shell(int *a,int n) { int i,j,k,x; k=n/2; /*间距值*/ while(k>=1) { for(i=k;i<n;i++) { x=a[i]; j=i-k; while(j>=0&&x<a[j]) { a[j+k]=a[j]; j-=k; } a[j+k]=x; } k/=2; /*缩小间距值*/ } } 上面我们已经对几种排序法作了介绍,现在让我们写个主函数检验一下。 #include<stdio.h> /*别偷懒,下面的"..."代表函数体,自己加上去哦!*/ void bubble(int *a,int n) { ... } void choise(int *a,int n) { ... } void quick(int *a,int i,int j) { ... } void insert(int *a,int n) { ... } void shell(int *a,int n) { ... } /*为了打印方便,我们写一个print吧。*/[code]void print(int *a,int n) { int i; for(i=0;i<n;i++) printf("%5d",a[i]); printf("\n"); } main() { /*为了公平,我们给每个函数定义一个相同数组*/ int a1[]={13,0,5,8,1,7,21,50,9,2}; int a2[]={13,0,5,8,1,7,21,50,9,2}; int a3[]={13,0,5,8,1,7,21,50,9,2}; int a4[]={13,0,5,8,1,7,21,50,9,2}; int a5[]={13,0,5,8,1,7,21,50,9,2}; printf("the original list:"); print(a1,10); printf("according to bubble:"); bubble(a1,10); print(a1,10); printf("according to choise:"); choise(a2,10); print(a2,10); printf("according to quick:"); quick(a3,0,9); print(a3,10); printf("according to insert:"); insert(a4,10); print(a4,10); printf("according to shell:"); shell(a5,10); print(a5,10); }
回复 使用道具 举报
  1. /**
  2.          * 冒泡排序
  3.          * 将int数组从小到大排序
  4.          *
  5.          * @param a
  6.          *            参数1:准备要排序的数组
  7.          * @return 返回:从小到大排序好了的数组
  8.          */
  9.         public static void paiXu(int[] a) {
  10.                 int m;
  11.                 for (int i = 0; i < a.length - 1; i++) {
  12.                         for (int j = 0; j < a.length - i - 1; j++) {

  13.                                 if (a[j] > a[j + 1]) {
  14.                                         m = a[j + 1];
  15.                                         a[j + 1] = a[j];
  16.                                         a[j] = m;

  17.                                 }

  18.                         }

  19.                 }

  20.         }

  21.         /**
  22.          * 查找排序
  23.          *
  24.          * @param a
  25.          *            要排序的数组
  26.          */
  27.         public static void sort(int[] a) {
  28.                 for (int i = 0; i < a.length - 1; i++) {
  29.                         int tm = i;
  30.                         for (int j = i + 1; j < a.length; j++) {
  31.                                 if (a[tm] > a[j]) {
  32.                                         tm = j;
  33.                                 }
  34.                         }
  35.                         if (tm != i) {
  36.                                 int m = a[i];
  37.                                 a[i] = a[tm];
  38.                                 a[tm] = m;
  39.                         }

  40.                 }
  41.         }
  42.         /**
  43.          * 选择排序
  44.          *
  45.          * @param a
  46.          *            要排序的数组
  47.          */
  48.         public static void xuanZeSort(int[] a) {
  49.                 for (int i = 0; i < a.length - 1; i++) {
  50.                
  51.                         for (int j = i + 1; j < a.length; j++) {
  52.                                 if (a[i] > a[j]) {
  53.                                         int m = a[i];
  54.                                         a[i] = a[j];
  55.                                         a[j] = m;
  56.                                 }
  57.                         }
  58.                        

  59.                 }
  60.         }
复制代码
回复 使用道具 举报
小麻 中级黑马 2015-5-26 22:16:12
9#
           学习下
回复 使用道具 举报
学习中1
回复 使用道具 举报
public class Student implements Comparable<Student>{         private String name;         private int age;         public  int compareTo(Student s) {                 int num = this.name.compareTo(s.name);                 return num==0?this.age-s.age:num;         }         public int hashCode(){                 return name.hashCode() + age*39;         }         public boolean equals(Object obj){                 if(obj == null)                         return false;                 if(this == obj)                         return true;                 if(obj instanceof Student){                         Student s = (Student)obj;                         return this.name.equals(s.name) && this.age==s.age;                 }                 return false;         }                  public String getName() {                 return name;         }         public void setName(String name) {                 this.name = name;         }         public int getAge() {                 return age;         }         public void setAge(int age) {                 this.age = age;         }         public Student(String name, int age) {                 super();                 this.name = name;                 this.age = age;         }                  public String toString(){                 return "Student "+name+".."+age;         } } class MyComparator implements Comparator<Student> {         public int compare(Student s1,Student s2){                 int age =s1.getAge() - s2.getAge();                 return age == 0?s1.getName().compareTo(s2.getName()):age;         } } public class TreeSetDemo2 {         public static void main(String[] args) {                 TreeSet<Student> ts = new TreeSet<Student>( new MyComparator());                 ts.add(new Student("a",18));                 ts.add(new Student("b",17));                 ts.add(new Student("c",15));                 ts.add(new Student("a",16));                 ts.add(new Student("b",21));                                  for(Student s : ts){                         System.out.println(s);                 }         } }这是集合里的元素排序方法,一个是比较器,另一个是对象里有自定的顺序
回复 使用道具 举报
代码写的不少,学习了
回复 使用道具 举报
xxz 中级黑马 2015-5-27 05:32:18
13#
/**
     * 直接选择排序
     */
    private static void directChooseSort ( int[] array )
    {
        for ( int i = 0; i < array.length; i++ )
        {
            int index = i;
            for ( int j = i + 1; j < array.length; j++ )
            {
                if (array[index] > array[j])
                {
                    index = j;
                }
            }
            if (i != index)
            {
                int temp = array[i];
                array[i] = array[index];
                array[index] = temp;
            }
        }
    }

    /**
     * 堆排序
     */
    private static void heapSort ( int[] array, int start, int len )
    {
        int pos = ( len - 1 ) / 2;
        for ( int i = pos; i >= 0; i-- )
        {
            int tmp = array[start + i];
            int index = i * 2 + 1;
            while (index < len)
            {
                if (index + 1 < len && array[start + index] < array[start + index + 1]) // 从小到大
                {
                    index += 1;
                }
                if (tmp < array[start + index]) // 从小到大
                {
                    array[start + i] = array[start + index];
                    i = index;
                    index = i * 2 + 1;
                }
                else
                {
                    break;
                }
            }
            array[start + i] = tmp;
        }
        for ( int i = 0; i < len; i++ )
        {
            int temp = array[start];
            array[start] = array[start + len - 1 - i];
            array[start + len - 1 - i] = temp;
            // 再一次
            int post = 0;
            int tmp = array[start + post];
            int index = 2 * post + 1;
            while (index < len - 1 - i)
            {
                if (index + 1 < len - 1 - i && array[start + index] < array[start + index + 1]) // 从小到大
                {
                    index += 1;
                }
                if (tmp < array[start + index]) // 从小到大
                {
                    array[start + post] = array[start + index];
                    post = index;
                    index = post * 2 + 1;
                }
                else
                {
                    break;
                }
            }
            array[start + post] = tmp;
        }
    }

    /**
     * 冒泡排序
     */
    private static void bubbleSort ( int[] array )
    {
        for ( int i = 0; i < array.length; i++ )
        {
            for ( int j = i + 1; j < array.length; j++ )
            {
                if (array[i] > array[j])
                {
                    int temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }
            }
        }
    }

    /**
     * 快速排序
     */
    private static void quickSort ( int[] array, int start, int end )
    {
        if (start < end)
        {
            int key = array[start];
            int i = start;
            for ( int j = start + 1; j < end + 1; j++ )
            {
                if (key > array[j])
                {
                    int temp = array[j];
                    array[j] = array[i + 1];
                    array[i + 1] = temp;
                    i++;
                }
            }
            array[start] = array[i];
            array[i] = key;
            quickSort (array, start, i - 1);
            quickSort (array, i + 1, end);
        }
    }
回复 使用道具 举报
来学习一下,
回复 使用道具 举报
本帖最后由 bin2015 于 2015-5-27 10:45 编辑

选择排序(直接排序) : 使用数组中的一个元素与其他位置的元素挨个比较一次,符合条件交换位置。

for(int j = 0 ; j<arr.length-1 ; j++ ){
                        //把最大值放在第0号位置
                        for(int i = j+1 ; i<arr.length ; i++){
                                if(arr[j]<arr){
                                        int temp = arr[j];
                                        arr[j] = arr;        
                                        arr = temp;
                                }
                        }
  }冒泡排序: 相邻的两个元素比较一次,符合条件交换 位置。
for(int j = 0 ; j<arr.length-1; j++){ // 疑问:arr.length-1。 因为五个数据只需要找出四个最大值即可排序
                        //把最大值放在倒数的第一个位置
                        for(int i = 0 ;  i < arr.length-1 - j ; i++){  //i=4  疑问: 因为for循环每执行完一次就 可以找出一个最大值,每找出一个最大值其实就可以少比较一次。
                                if(arr>arr[i+1]){
                                        //交换位置
                                        int temp  = arr;
                                        arr = arr[i+1];
                                        arr[i+1] = temp;
                                }
                        }
}


回复 使用道具 举报
学习学习
回复 使用道具 举报
插入排序 1.直接插入排序 原理:将数组分为无序区和有序区两个区,然后不断将无序区的第一个元素按大小顺序插入到有序区中去,最终将所有无序区元素都移动到有序区完成排序。 要点:设立哨兵,作为临时存储和判断数组边界之用。 实现: Void InsertSort(Node L[],int length) { Int i,j;//分别为有序区和无序区指针 for(i=1;i<length;i++)//逐步扩大有序区 { j=i+1; if(L[j]<L[i]) { L[0]=L[j];//存储待排序元素 While(L[0]<L[i])//查找在有序区中的插入位置,同时移动元素 { L[i+1]=L[i];//移动 i--;//查找 } L[i+1]=L[0];//将元素插入 } i=j-1;//还原有序区指针 } } 2.希尔排序 原理:又称增量缩小排序。先将序列按增量划分为元素个数相同的若干组,使用直接插入排序法进行排序,然后不断缩小增量直至为1,最后使用直接插入排序完成排序。 要点:增量的选择以及排序最终以1为增量进行排序结束。 实现: Void shellSort(Node L[],int d) { While(d>=1)//直到增量缩小为1 { Shell(L,d); d=d/2;//缩小增量 } } Void Shell(Node L[],int d) { Int i,j; For(i=d+1;i<length;i++) { if(L[i]<L[i-d]) { L[0]=L[i]; j=i-d; While(j>0&&L[j]>L[0]) { L[j+d]=L[j];//移动 j=j-d;//查找 } L[j+d]=L[0]; } } } 交换排序 1.冒泡排序 原理:将序列划分为无序和有序区,不断通过交换较大元素至无序区尾完成排序。 要点:设计交换判断条件,提前结束以排好序的序列循环。 实现: Void BubbleSort(Node L[]) { Int i ,j; Bool ischanged;//设计跳出条件 For(j=n;j<0;j--) { ischanged =false; For(i=0;i<j;i++) { If(L[i]>L[i+1])//如果发现较重元素就向后移动 { Int temp=L[i]; L[i]=L[i+1]; L[i+1]=temp; Ischanged =true; } } If(!ischanged)//若没有移动则说明序列已经有序,直接跳出 Break; } } 2.快速排序 原理:不断寻找一个序列的中点,然后对中点左右的序列递归的进行排序,直至全部序列排序完成,使用了分治的思想。 要点:递归、分治 实现:  选择排序 1.直接选择排序 原理:将序列划分为无序和有序区,寻找无序区中的最小值和无序区的首元素交换,有序区扩大一个,循环最终完成全部排序。 要点: 实现: Void SelectSort(Node L[]) { Int i,j,k;//分别为有序区,无序区,无序区最小元素指针 For(i=0;i<length;i++) { k=i; For(j=i+1;j<length;j++) { If(L[j]<L[k]) k=j; } If(k!=i)//若发现最小元素,则移动到有序区 { Int temp=L[k]; L[k]=L[i]; L[i]=L[temp]; }   } } 2.堆排序 原理:利用大根堆或小根堆思想,首先建立堆,然后将堆首与堆尾交换,堆尾之后为有序区。 要点:建堆、交换、调整堆 实现: Void HeapSort(Node L[]) { BuildingHeap(L);//建堆(大根堆) For(int i=n;i>0;i--)//交换 { Int temp=L[i]; L[i]=L[0]; L[0]=temp; Heapify(L,0,i);//调整堆 } }  Void BuildingHeap(Node L[]) { For(i=length/2 -1;i>0;i--) Heapify(L,i,length); } 归并排序 原理:将原序列划分为有序的两个序列,然后利用归并算法进行合并,合并之后即为有序序列。 要点:归并、分治 实现: Void MergeSort(Node L[],int m,int n) { Int k; If(m<n) { K=(m+n)/2; MergeSort(L,m,k); MergeSort(L,k+1,n); Merge(L,m,k,n); } }  基数排序 原理:将数字按位数划分出n个关键字,每次针对一个关键字进行排序,然后针对排序后的序列进行下一个关键字的排序,循环至所有关键字都使用过则排序完成。 要点:对关键字的选取,元素分配收集。 实现: Void RadixSort(Node L[],length,maxradix) { Int m,n,k,lsp; k=1;m=1; Int temp[10][length-1]; Empty(temp); //清空临时空间 While(k<maxradix) //遍历所有关键字 { For(int i=0;i<length;i++) //分配过程 { If(L[i]<m) Temp[0][n]=L[i]; Else Lsp=(L[i]/m)%10; //确定关键字 Temp[lsp][n]=L[i]; n++; } CollectElement(L,Temp); //收集 n=0; m=m*10; k++; } }
回复 使用道具 举报
来观看观看,学习下
回复 使用道具 举报
二叉树排序:package ye;

public class Ye_erchashu {
        public static class BinaryNode {  
                 
                    private int value;//current value  
                    private BinaryNode lChild;//left child  
                    private BinaryNode rChild;//right child  
                     
                    public BinaryNode(int value, BinaryNode l, BinaryNode r){  
                        this.value = value;  
                        this.lChild = l;  
                        this.rChild = r;  
                    }  
                     
                    public BinaryNode getLChild() {  
                        return lChild;  
                    }  
                    public void setLChild(BinaryNode child) {  
                        lChild = child;  
                    }  
                    public BinaryNode getRChild() {  
                        return rChild;  
                    }  
                    public void setRChild(BinaryNode child) {  
                        rChild = child;  
                    }  
                    public int getValue() {  
                        return value;  
                    }  
                    public void setValue(int value) {  
                        this.value = value;  
                    }  
                     
                    //iterate all node.  
                    public static void iterate(BinaryNode root){  
                        if(root.lChild!=null){  
                            iterate(root.getLChild());  
                        }  
                        System.out.print(root.getValue() + " ");  
                        if(root.rChild!=null){  
                            iterate(root.getRChild());  
                        }  
                    }  
                     
                    /**
                     * add child to the current node to construct a tree.
                     * Time: O( nlog(n) )
                     * **/  
                    public void addChild(int n){  
                        if(n<value){  
                            if(lChild!=null){  
                                lChild.addChild(n);  
                            }  
                            else{  
                                lChild = new BinaryNode(n, null, null);  
                            }  
                        }  
                        else{  
                            if(rChild!=null){  
                                rChild.addChild(n);  
                            }  
                            else{  
                                rChild = new BinaryNode(n, null, null);  
                            }  
                        }  
                    }  
                     
                    //test case.  
                    public static void main(String[] args){  
                        System.out.println();  
                        int[] arr = new int[]{23,54,1,65,9,3,100};  
                        BinaryNode root = new BinaryNode(arr[0], null, null);  
                        for(int i=1; i<arr.length; i++){  
                            root.addChild(arr[i]);  
                        }  
                        BinaryNode.iterate(root);  
                    }  
                }
}
回复 使用道具 举报
一、排序主要分为:简单基础排序和快速排序。
1.简单基础排序:冒牌排序,插入排序等。
2.快速排序:快速排序,堆排序,基数排序,shell排序等。
二、排序实现代码分为C语言和Java语言,来进行实现。市场或者是大学数据结构书籍都是用的是C语言进行举例的,所以不清楚版主想用那种语言实现。如果实在不行可以去当当网买一本Java算法分析或者Java数据结构都有对排序进行详细的讲解。希望对题主有意义。
回复 使用道具 举报
12下一页
您需要登录后才可以回帖 登录 | 加入黑马