Java中的冒泡排序算法
package cn.edu.hactcm;
/**
* 冒泡排序算法
*/
public class BubbleSortDemo {
public static void main(String[] args) {
int[] r = { 22, 12, 34, 123, 65, 34, 65, 34, 567, 3, 65, 546, 4 };
BubbleSort(r);
for (int i : r) {
System.out.print(i + " ");
}
}
private static void BubbleSort(int[] r) {
Boolean exchange;
int tmp;
for (int i = 1; i < r.length;) {
//最多做n-1趟排序
exchange = false; //本趟排序开始前,交换标志应为假
for (int j = 0; j < r.length - i; j++) {
if (r[j] > r[j+1]) {
//交换记录
tmp = r[j + 1];
r[j + 1] = r[j];
r[j] = tmp;
exchange = true;//当发生了交换,提前终止算法
}
}
if (!exchange) {
return;
}
}
}
}
Java中插入排序算法
package cn.edu.hactcm;
/**
* 插入排序算法
*/
public class InsertSortDemo01 {
public static void main(String[] args) {
int r[] = { 110, 3, 23, 23, 231, 342, 45 };
System.out.println("直接插入排序后的结果为:");
InsertSort(r);
for (int i = 0; i < r.length; i++) {
System.out.print(r[i] + " ");
}
}
public static void InsertSort(int[] r) {
for (int i = 1; i < r.length; i++) { // 注意此处从1开始的
if (r[i] < r[i - 1]) {//如果后者小于前者
int temp = r[i]; // 要排序的数字
int j = 0;
for (j = i - 1; j >= 0 && temp < r[j]; j--) {
r[j + 1] = r[j]; // 将数据后移
}
// 若不符合上面的情况时让数组的i个的值为temp
r[j + 1] = temp;
}
}
}
}
快排:
package cn.edu.hactcm;
/**
* 快排算法
*/
public class QuickSortDemo {
public static void main(String[] args) {
int R[] = { 22, 12, 34, 123, 65, 34, 65, 34, 567, 3, 65, 45, 546, 4 };
quickSort(R, 0, 13);
for (int i = 0; i < R.length; i++) {
System.out.print(R[i] + " ");
}
}
public static void quickSort(int[] R, int low, int high) {
if (low > high)
return;
int pivot = R[low];
int i = low;
int j = high;
while (i < j) {
while (i < j && R[j] >= pivot)
j--;
R[i] = R[j]; // 将后面的值赋给R[i],使小的数值排在前面
while (i < j && R[i] <= pivot)
i++;
R[j] = R[i]; // 当前面的数值中有大于pivot的数值时,将之后排
}
R[i] = pivot; // 将空出来的位置放上pivot
quickSort(R, low, i - 1);
quickSort(R, i + 1, high);
}
}
Java中选择排序
package cn.edu.hactcm;
/**
* 选择排序算法
*/
public class SelectSortDemo {
public static void main(String[] args) {
int r[] = { 22, 12, 34, 123, 65, 34, 65, 34, 567, 3, 65, 45, 546, 4 };
SelectSort(r);
for (int i = 0; i < r.length; i++) {
System.out.print(r[i] + " ");
}
}
/**
* 选择排序
* @param r
*/
public static void SelectSort(int[] r) {
int i, j, k;
int temp;
for (i = 0; i < r.length - 1; i++) {
k = i; // 最开始是将k的值赋给要比较的数字
for (j = i + 1; j < r.length; j++) {
if (r[j] < r[k]) {
k = j; // k记下目前找到的最小关键字所在的位置
}
}
if (k != i) { // 一定要记住是k和i之间的比较
temp = r[i];
r[i] = r[k];
r[k] = temp;
}
}
}
}
package cn.edu.hactcm;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 查找元素的位置
*/
public class TheFirstOne {
public static void main(String[] args) {
int[] temp = { 10, 20, 32, 2, 565, 232, 54, 67, 34, 0 };
System.out.print("请输入你要找的值:");
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in));
int key = 0;
try {
key = Integer.parseInt(buf.readLine());
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if (buf != null) {
try {
buf.close();
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
buf = null;
}
System.out.println("您要查找的元素在数组中的位置为:" + seqSearch(temp, key));
}
/**
*查询算法
*/
public static int seqSearch(int temp[], int key) {
int i;
for (i = 0; temp[i] != key; i++);
if (i == temp.length) {
return -1;
} else {
return i;
}
}
}
package cn.edu.hactcm;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 查找元素的位置
*/
public class TheSecondOne {
public static void main(String[] args) {
int[] temp = { 10, 20, 32, 2, 565, 232, 54, 67, 34, 0 };
System.out.print("请输入你要找的值:");
BufferedReader buf = new BufferedReader(
new InputStreamReader(System.in));
int key = 0;
try {
key = Integer.parseInt(buf.readLine());
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if (buf != null) {
try {
buf.close();
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
buf = null;
}
System.out.println("您要查找的元素在数组中的位置为:" + binSearch(temp, key));
}
/**
* 中分查找
* @param temp
* @param key :我们要找的值
* @return
*/
public static int binSearch(int[] temp, int key) {
int low = 0, high = temp.length - 1, mid; // high表示数组最后一个的下标
while (low <= high) {
mid = (low + high) / 2;
//如果中间值恰好为我们要找的值
if (temp[mid] == key)
return mid;
if (temp[mid] > key)
high = mid - 1;
else
low = mid + 1;
}
return -1; // 当low》high时表示查找区间为空,查找失败
}
}