隨筆 - 64  文章 - 9  trackbacks - 0
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(6)

          我參與的團(tuán)隊(duì)

          隨筆分類(88)

          隨筆檔案(92)

          文章分類(142)

          文章檔案(182)

          天基成員

          學(xué)習(xí)園

          我的海角

          搜索

          •  

          積分與排名

          • 積分 - 183435
          • 排名 - 319

          最新評論

          class Linear
          {
           int size;
           int num;
           int data[];

           //申請空間,如果用戶輸入的空間大小小于等于0,則默認(rèn)使用空間100
           public Linear(int n)
           {
            if(n <= 0)
             size = 100;

            else
             size = n;

            data = new int[size];
            num = 0;
           }
           //遍歷所有的數(shù)據(jù),并輸出
           public void Visit()
           {
            if(num == 0)
            {
             System.out.println("Visit:The array is empty!");
             return;
            }

            for(int i = 0; i < num; i++)
             System.out.print(data[i]+"\t");

            System.out.println();
           }
           //插入一個(gè)數(shù)據(jù)
           public void Insert(int pos,int x)
           {
            if(pos < 0 || pos > num)
            {
             System.out.println("Insert:The position is error!");
             return;
            }
            
            if(num == size)
            {
             System.out.println("Insert:The array is full!");
             return;
            }

            for(int i = num - 1; i >= pos; i--)
            {
             data[i+1] = data[i];
            }

            data[pos] = x;

            num ++;
           }
           //刪除一個(gè)數(shù)據(jù)
           public void Delete(int pos)
           {
            if(pos < 0 || pos > (num - 1))
            {
             System.out.println("Delete:The position is error!");
             return;
            }

            if(num == 0)
            {
             System.out.println("Delete:The array is empty!");
             return;
            }

            for(int i = pos; i < (num - 1); i++)
             data[i] = data[i+1];

            num -- ;
           }
           //選擇排序法,從小到大排列
           public void SelectSort()
           {
            int k = 0,p = 0,t = 0;

            for(int i = 0; i < num -1; i++)
            {
             t = data[i];
             k = i;
             p = i;

             for(int j = i + 1; j < num; j++)
              if(t > data[j])
              {
               t = data[j];
               p = j;
              }

             if(k != p)
             {
              int temp = data[k];
              data[k] = data[p];
              data[p] = temp;
             }
            }
           }
           //插入排序法,從小到大
           public void InserSort()
           {
            int i = 0,j = 0;

            for(i = 0; i < num; i++)
            {
             int t = data[i]; 
            
             for(j = i - 1; j >= 0; j --)
             {
              if(t < data[j])
               data[j+1] = data[j];
              else
               break;
             }

             data[j+1] = t;
            }
           }
           //冒泡排序
           public void BubbleSort()
           {
            int t = 0;

            for(int i = 0; i < num -1; i++)
             for(int j = i+1; j < num; j++)
              if(data[i] > data[j])
              {
               t = data[i];
               data[i] = data[j];
               data[j] = t;
              }
           }
           //快速排序,因?yàn)橐褂眠f歸,因此必須定義一個(gè)額外的快排方法
           private void qsort(int low,int high)
           {
            int i = 0, j = 0 ,t = 0;
            
            if(low < high)
            {
             i = low;
             j = high;
             t = data[low];

             while(i < j)
             {
              while((i < j) && (data[j] > t)) j--;

              if(i < j)
               data[i++] = data[j];

              while((i < j) && (data[i] <= t)) i++;

              if( i < j)
               data[j--] = data[i];
             }

             data[i] = t;

             qsort(low,j - 1);
             qsort(j + 1,high);
            }
           }

           public void QuickSort()
           {
            qsort(0,num - 1);
           }
          }

          class Program
          {
           public static void main(String arg[])
           {
            //使用Linear線形結(jié)構(gòu)
            Linear line = new Linear(5);

            //初始化若干數(shù)據(jù),即插入5條數(shù)據(jù)

            line.Insert(0,3);
            line.Insert(1,7);
            line.Insert(2,2);
            line.Insert(3,5);
            line.Insert(4,3);

            //line.SelectSort();
            //line.InserSort();
            //line.BubbleSort();
            line.QuickSort();

            line.Visit();

            
           }
          }

          posted on 2009-09-16 12:02 鵬凌 閱讀(164) 評論(0)  編輯  收藏 所屬分類: Java --j2ee
          主站蜘蛛池模板: 仁化县| 康马县| 灯塔市| 成都市| 东莞市| 长垣县| 景德镇市| 屏东市| 桐柏县| 稻城县| 包头市| 河池市| 黄陵县| 洛川县| 凤山市| 安岳县| 巫山县| 湖南省| 重庆市| 类乌齐县| 灯塔市| 黄龙县| 虹口区| 塘沽区| 宣汉县| 万安县| 顺义区| 炉霍县| 额尔古纳市| 桓台县| 亚东县| 奉化市| 望谟县| 陈巴尔虎旗| 遂溪县| 图木舒克市| 射阳县| 公安县| 六枝特区| 巴塘县| 岐山县|