package com.sort;
public class Sort {
public static void main(String[] args) {
// nineNineMultiply();
int ary[] = new int[] { 6, 2, 8, 1, 0 };
// print(ary);
// insertSort(ary);
// bubbleSort(ary);
// System.out.println(binarySearch(ary,1));
// selectSort(ary);
System.out.println("5的阶乘是 :" + factorial(5));
}
/**
* 九九乘法表
*/
public static void nineNineMultiply() {
for (int i = 1, j = 1; j < 10; i++) {
System.out.print(i + "*" + j + "=" + i * j + " ");
if (i == j) {
i = 0;
j++;
System.out.println();
}
}
}
/**
* 冒泡排序
* 冒泡排序(BubbleSort)的基本概念是:依次比较相邻的两个数,将小数放在前面,大数放在后面。即首先比较第1个和第2个数,将小数放前
* ,大数放后。然后比较第2个数和第3个数,将小数放前,大数放后,如此继续,直至比较最后两个数,将小数放前,大数放后。
* 由于在排序过程中总是小数往前放,大数往后放,相当于气泡往上升,所以称作冒泡排序。
*/
public static void bubbleSort(int ary[]) {
int temp;
for (int i = 0; i < ary.length; i++) {
for (int j = i + 1; j < ary.length; j++) {
if (ary[j] < ary[i]) {
temp = ary[j];
ary[j] = ary[i];
ary[i] = temp;
}
}
}
print(ary);
}
/**
*插入排序 每次处理就是将无序数列的第一个元素与有序数列的元素从后往前逐个进行比较,找出插入位置,将该元素插入到有序数列的合适位置中。
*/
public static void insertSort(int ary[]) {
int temp;
for (int i = 1; i < ary.length; i++) {
temp = ary[i];
while (i > 0 && temp < ary[i - 1]) {
ary[i] = ary[i - 1];
--i;
}
ary[i] = temp;
}
print(ary);
}
/**
* 遍历数组
*/
public static void print(int ary[]) {
for (int i : ary) {
System.out.print(i + " ");
}
System.out.println();
System.out.println("***********************");
}
/**
* 二分查找
*
* @param 查找的数组
* @param 查找的数
* @return 该数在数组中的位置(下标)
*
*
*
*
*
*
*
* 【算法思想】首先,将表中间位置记录的关键字与查找关键字比较,如果两者相等,则查找成功;否则利用中间位置记录将表分成前、后两个子表,
* 如果中间位置记录的关键字大于查找关键字,则进一步查找前一子表,否则进一步查找后一子表。
*/
public static int binarySearch(int[] ary, int num) {
insertSort(ary);// 排序
int low = 0;
int high = ary.length - 1;
int postion = (low + high) / 2;
while (low <= high) {
if (ary[postion] == num) {
return postion;
} else if (ary[postion] < num) {
low = postion + 1;
} else {
high = postion - 1;
}
postion = (low + high) / 2;
}
return -1;
}
/**
* 选择排序 每一趟从待排序的数据元素中选出最小(或最大)的一个元素,顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。
* 选择排序是不稳定的排序方法。
*
* @param ary
*/
public static void selectSort(int ary[]) {
int temp, min, minIndex;
for (int i = 0; i < ary.length; i++) {
minIndex = i;
for (int j = i + 1; j < ary.length; j++) {
min = ary[minIndex];
temp = ary[j];
if (temp > min) {
minIndex = j;
}
}
if (minIndex != i) {
temp = ary[minIndex];
ary[minIndex] = ary[i];
ary[i] = temp;
}
}
print(ary);
}
/**
* 递归
* 递归做为一种算法在程序设计语言中广泛应用.是指函数/过程/子程序在运行过程中直接或间接调用自身而产生的重入现象.
*/
public static int factorial(int num) {
if (num == 1) {
return 1;
}
return num * factorial(num - 1);
}
}
public class Sort {
public static void main(String[] args) {
// nineNineMultiply();
int ary[] = new int[] { 6, 2, 8, 1, 0 };
// print(ary);
// insertSort(ary);
// bubbleSort(ary);
// System.out.println(binarySearch(ary,1));
// selectSort(ary);
System.out.println("5的阶乘是 :" + factorial(5));
}
/**
* 九九乘法表
*/
public static void nineNineMultiply() {
for (int i = 1, j = 1; j < 10; i++) {
System.out.print(i + "*" + j + "=" + i * j + " ");
if (i == j) {
i = 0;
j++;
System.out.println();
}
}
}
/**
* 冒泡排序
* 冒泡排序(BubbleSort)的基本概念是:依次比较相邻的两个数,将小数放在前面,大数放在后面。即首先比较第1个和第2个数,将小数放前
* ,大数放后。然后比较第2个数和第3个数,将小数放前,大数放后,如此继续,直至比较最后两个数,将小数放前,大数放后。
* 由于在排序过程中总是小数往前放,大数往后放,相当于气泡往上升,所以称作冒泡排序。
*/
public static void bubbleSort(int ary[]) {
int temp;
for (int i = 0; i < ary.length; i++) {
for (int j = i + 1; j < ary.length; j++) {
if (ary[j] < ary[i]) {
temp = ary[j];
ary[j] = ary[i];
ary[i] = temp;
}
}
}
print(ary);
}
/**
*插入排序 每次处理就是将无序数列的第一个元素与有序数列的元素从后往前逐个进行比较,找出插入位置,将该元素插入到有序数列的合适位置中。
*/
public static void insertSort(int ary[]) {
int temp;
for (int i = 1; i < ary.length; i++) {
temp = ary[i];
while (i > 0 && temp < ary[i - 1]) {
ary[i] = ary[i - 1];
--i;
}
ary[i] = temp;
}
print(ary);
}
/**
* 遍历数组
*/
public static void print(int ary[]) {
for (int i : ary) {
System.out.print(i + " ");
}
System.out.println();
System.out.println("***********************");
}
/**
* 二分查找
*
* @param 查找的数组
* @param 查找的数
* @return 该数在数组中的位置(下标)
*
*
*
*
*
*
*
* 【算法思想】首先,将表中间位置记录的关键字与查找关键字比较,如果两者相等,则查找成功;否则利用中间位置记录将表分成前、后两个子表,
* 如果中间位置记录的关键字大于查找关键字,则进一步查找前一子表,否则进一步查找后一子表。
*/
public static int binarySearch(int[] ary, int num) {
insertSort(ary);// 排序
int low = 0;
int high = ary.length - 1;
int postion = (low + high) / 2;
while (low <= high) {
if (ary[postion] == num) {
return postion;
} else if (ary[postion] < num) {
low = postion + 1;
} else {
high = postion - 1;
}
postion = (low + high) / 2;
}
return -1;
}
/**
* 选择排序 每一趟从待排序的数据元素中选出最小(或最大)的一个元素,顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。
* 选择排序是不稳定的排序方法。
*
* @param ary
*/
public static void selectSort(int ary[]) {
int temp, min, minIndex;
for (int i = 0; i < ary.length; i++) {
minIndex = i;
for (int j = i + 1; j < ary.length; j++) {
min = ary[minIndex];
temp = ary[j];
if (temp > min) {
minIndex = j;
}
}
if (minIndex != i) {
temp = ary[minIndex];
ary[minIndex] = ary[i];
ary[i] = temp;
}
}
print(ary);
}
/**
* 递归
* 递归做为一种算法在程序设计语言中广泛应用.是指函数/过程/子程序在运行过程中直接或间接调用自身而产生的重入现象.
*/
public static int factorial(int num) {
if (num == 1) {
return 1;
}
return num * factorial(num - 1);
}
}