排序算法小结

来源:互联网 发布:天天向上网络作家 编辑:程序博客网 时间:2024/05/16 05:16
 

                                                   

import java.io.*;

 

public class Paixu {

     // 冒泡排序法

     基本思想:两两比较待比较元素需要交换位置时,交换位置.

     public void Maopao(int a[]) {

          for (int i = 1; i < a.length; i++) {

              for (int j = 0; j < a.length - i; j++) {

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

                        int temp = a[j + 1];

                        a[j + 1] = a[j];

                        a[j] = temp;

                   }

              }

          }

          System.out.println("\n" + "采用冒泡排序法:");

     }

 

     // 插入排序法:

 //每次把一个待排序的元素插入到前面已经排好序的序列中

     public void Charu(int a[]) {

          for (int i = 1; i < a.length; i++) {

              for (int j = 0; j < i; j++) {

                   if (a[j] > a[i]) {

                        int temp = a[i];

                        for (int k = i; k > j; k--) {

                             a[k] = a[k--];

                        }

                        a[j] = temp;

                   }

              }

          }

          System.out.println("\n" + "采用插入排序法:");

     }

 

    

 

// 选择排序法:

//从待排序的数组中挑出最小或是最大的发在已排好序的数组最后

     public void Xuanze(int a[]) {

          for (int i = 0; i < a.length; i++) {

              int position = i;

              int temp = a[position];

          /以a[i]为参照,选择

              for (int j = i + 1; j < a.length; j++) {

                   if (temp > a[j]) {

                        temp = a[j];

                       position = j;

                   }

              }

            a[position]= a[i];

           a[i] =temp;

          }

          System.out.println("\n" + "采用选择排序法:");

     }

 

     public void Print(int a[]) {

          System.out.println("从小到大排序结果为:");

          for (int i = 0; i < a.length; i++) {

              System.out.print(a[i] + ",");

          }

     }

 

     public static void main(String[] args) {

          int a[] = new int[5];

          Paixu px = new Paixu();

          BufferedReader buf = new BufferedReader(

                   new InputStreamReader(System.in));

          System.out.println("请输入五个整数:");

          for (int i = 0; i < a.length; i++) {

              try {

                   String s = buf.readLine();

                   int j = Integer.parseInt(s);

                   a[i] = j;

              } catch (Exception e) {

                   System.out.println("出错了!必须输入整数,请重新输入!");

                   i--;

              }

          }

          System.out.println("您输入的整数依次为:");

          for (int i = 0; i < a.length; i++) {

              System.out.print(a[i] + ",");

          }

          System.out.println("\n" + "-------------");

          px.Maopao(a); // 调用冒泡算法

          px.Print(a);

          System.out.println("\n" + "-------------");

          px.Charu(a); // 调用插入算法

          px.Print(a);

          System.out.println("\n" + "-------------");

          px.Xuanze(a); // 调用选择算法

          px.Print(a);

     }

}

 

 

Java实现二分查找

2008-11-19 21:38

今天阿朗被问到二分查找竟然一着急没写出来。faint =。= 回来复习下

import java.util.*;

public class BinarySearch {

public static void main(String[] args) {
   ArrayList<Integer> a = new ArrayList<Integer>();
   addIntegerInSequence(a,1,10);
   print(a);
   int pos = binarySearch(a,10);
   if ( pos != -1 )
   {
    System.out.print("Element found: " + pos);
   }
   else
   {
    System.out.print("Element not found");
   }
}

/**
* 二分查找法
* @param a
* @param value 待查找元素
* @return
*/
public static int binarySearch(ArrayList<Integer> a, int value)
{
   int size = a.size();
   int low = 0 , high = size - 1;
   int mid;
   while (low <= high)
   {
    mid = (low + high) / 2;
    if ( a.get(mid) < value )
    {
     low = low + 1;
    }
    else if ( a.get(mid) > value )
    {
     high = high - 1;
    }
    else
    {
     return mid;
    }
   }
   return -1;
}

/**
* 填充顺序元素到数组
* @param a
* @param begin 开始元素
* @param size 大小
*/
public static void addIntegerInSequence(ArrayList<Integer> a, int begin, int size)
{
   for (int i = begin; i < begin + size; i++)
   {
    a.add(i);
   }
}

/**
* 打印数组
* @param a
*/
public static void print(ArrayList<Integer> a)
{
   Iterator<Integer> i = a.iterator();
   while (i.hasNext())
   {
    System.out.print(i.next() + " ");
   }
   System.out.println("");
}

}

 

/////

JAVA 库中的二分查找使用非递归方式实现,返回结果与前面写的有所不同:找不到时返回的是负数,但不一定是-1

 private static int binarySearch0(int[] a, int fromIndex, int toIndex,

                     int key) {

    int low = fromIndex;

    int high = toIndex - 1;

 

    while (low <= high) {

        int mid = (low + high) >>> 1;

        int midVal = a[mid];

 

        if (midVal < key)

        low = mid + 1;

        else if (midVal > key)

        high = mid - 1;

        else

        return mid; // key found

    }

    return -(low + 1);  // key not found.

    }

 

 

 

原创粉丝点击