/**
* @Description:TODO
* @project:project
* @class:BubbleSort.java
* @author:
* @time:2015-7-14 上午11:08:44
*/
package sort;
/**
* @deprecated: 冒泡排序
* @author:
*/
public class BubbleSort {
public static void main(String[] args) {
int score[] = { 67, 69, 75, 87, 89, 90, 99, 100 };
for (int i = 0; i < score.length - 1; i++) {
for (int j = 0; j < score.length - i - 1; j++) {
if (score[j] < score[j + 1]) {
int temp = score[j];
score[j] = score[j + 1];
score[j + 1] = temp;
}
}
}
System.out.print("排序的结果:");
for (int a = 0; a < score.length; a++) {
System.out.print(score[a] + " ");
}
}
}
2,插入排序
/**
* @Description:TODO
* @project:project
* @class:InsertSort.java
* @author:
* @time:2015-7-14 下午02:56:32
*/
package sort;
/**
* @deprecated: 插入排序
* @author:
*/
public class InsertSort {
public static void main(String[] args) {
int array[] = { 4, 12, 3, 1, 9 };
insertSort(array);
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
if (i != array.length - 1) {
System.out.print(",");
}
}
}
public static void insertSort(int[] array) {
for (int i = 1; i < array.length; i++) {
if (array[i] < array[i - 1]) {
// 保存第i位的值
int temp = array[i];
int k = i - 1;
for (int j = k; j >= 0 && temp < array[j]; j--) {
array[j + 1] = array[j];
k--;
}
array[k + 1] = temp;
}
}
}
}
3,快速排序
/**
* @Description:TODO
* @project:project
* @class:QuickSort.java
* @author:
* @time:2015-7-14 上午11:24:55
*/
package sort;
/**
* @deprecated: 快速排序
* @author:
*/
public class QuickSort {
public static void main(String[] args) {
int score[] = { 100,69 , 67 , 75, 89, 87, 90, 99 };
quick_sort(score,0,score.length-1);
for (int a = 0; a < score.length; a++) {
System.out.print(score[a] + " ");
}
}
// 快速排序
public static void quick_sort(int s[], int l, int r) {
if (l < r) {
int i = l;
int j = r;
int x = s[l];
while (i < j) {
while (i < j && s[j] >= x)
j--;
if (i < j)
s[i++] = s[j];
while (i < j && s[i] < x)
i++;
if (i < j)
s[j--] = s[i];
}
s[i] = x;
quick_sort(s, l, i - 1); // 递归调用
quick_sort(s, i + 1, r);
}
}
}
4,选择排序
/**
* @Description:TODO
* @project:project
* @class:SelectionSort.java
* @author:wangK
* @time:2015-7-14 下午02:31:18
*/
package sort;
/**
* @deprecated: 选择排序
* @author: wangK
*/
public class SelectionSort {
public static void main(String[] args) {
int a[] = { 2, 13, 4, 1, 9 };
sort(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
public static void sort(int array[]) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
int k = i;
for (int j = i + 1; j < n; j++) {
if (array[j] < array[k]) {
k = j;
}
}
int t = array[k];
array[k] = array[i];
array[i] = t;
}
}
}