经常在笔试(或面试)中出现的JAVA经典算法,本人特此整理,希望有用
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; } } } } // 插入排序法: 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; } } } } // 选择排序法: public void Xuanze(int a[]) { for (int i = 0; i < a.length; i++) { int position = i; for (int j = i + 1; j < a.length; j++) { System.out.println("\n" + "采用插入排序法:"); System.out.println("\n" + "采用冒泡排序法:");
经常在笔试(或面试)中出现的JAVA经典算法,本人特此整理,希望有用
} } } if (a[position] > a[j]) { int temp = a[position]; a[position] = a[j]; a[j] = 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] + ","); }
经常在笔试(或面试)中出现的JAVA经典算法,本人特此整理,希望有用
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 待查找元素
经常在笔试(或面试)中出现的JAVA经典算法,本人特此整理,希望有用
* @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
*/
经常在笔试(或面试)中出现的JAVA经典算法,本人特此整理,希望有用
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.
}