啊哈算法
《啊哈算法》的Java实现 | 第六章 :最短路径及最短路径算法的对比分析.
第一章:排序
桶排序
步骤:
1.申请一个大小为n的数组a[n],并将数组初始化为0
2.输入数组,当输入第一个数字为5时,就在对应的a[5]的值在原来的基础上+1,即将a[5]的值从0改为1
3.从大到下,倒序进行输出
package ch1;
import java.util.Scanner;
public class Test01 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int []book = new int[1001]; //开辟一个数组
for(int i =0;i <= 1000; i++){
book[i] = 0;//初始化数组为0
}//此处循环了1001次
int t =0;
int n = sc.nextInt();//输入一共要输入多少个数
for(int i = 0; i < n; i++){
t = sc.nextInt();
book[t]++;//把输入的数字在数组中数量+1
}//此处循环了n次
for(int i = 1000; i>= 0; i--){//倒序打印
for(int j =1; j<=book[i];j++){//打印的次数
System.out.printf("%d ",i);//此处循环了1001+n次
}
}
}
}
//一共循环了2n+2002次
//算法的时间复杂度为O(2n+2002)我们再说时间复杂度的时候可以忽略较小的常数,所以算法的时间复杂度为O(M+N)
冒泡排序
原理:每次比较两个相邻元素,如果它的顺序错误就交换
核心部分为双层嵌套
package ch1;
import java.util.Scanner;
public class Test02 {
public void bubbleSort(int n,int[] a){
int temp;
for(int i = 0;i<n;i++){ // 从头开始循环
for(int j = 0; j<n-i-1;j++){ //比较过的不再进行比较
if(a[j]<a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); //输入一共有多少的数字
int[] a = new int[n]; //定义一个数组的容量为n+1
for(int i = 0;i<n;i++){
a[i] = sc.nextInt(); // 将输入的数字存入到数组当中
}
sc.close();
Test02 test02 = new Test02();//创建对象调用函数
test02.bubbleSort(n,a); //相邻的数进行比较
int flag = 0;
for(int i = 0;i<n;i++){
if(flag == 0){
System.out.print(a[i]);
flag = 1;
}else {
System.out.print(" " + a[i]);
}
}
}
}
//时间复杂度为O(N^2),时间复杂度过高
快速排序
1.首先找出这个序列的基准是从0下标开始
2.从右边开始,从右往左找到小于基准数的数,再从左往右找到大于基准数的数,并将两数进行交换
3.利用递归算法全部查找完毕,将所有数归位
package ch1;
import java.util.Arrays;
import java.util.Scanner;
public class Test03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();//一共多少数据
int[] a = new int[n];
for (int i= 0;i < n;i++){
a[i] = scanner.nextInt();
}
quickSort(a,0,n-1);
//输出结果
for (int i =0; i< n;i++){
System.out.print(a[i]+" ");
}
/*int[] arrays = {6, 1, 2, 7, 9, 3, 4, 5, 10, 8};
quickSort(arrays, 0, arrays.length-1);
System.out.println(Arrays.toString(arrays));*/
}
public static void quickSort(int a[], int left, int right){
int i ,j, t ,temp;
if (left > right){//边界条件
return;
}
temp = a[left];//存的为基准数
i = left;
j = right;
while(i!=j){
//首先从右往左开始找
while(a[j]>= temp && i<j){
j--;
}
while(a[i] <= temp && i<j){
i++;
}
//程序运行到此处说明i处的值大于了基准值,j处的值小于了基准值
//这个时候需要交换
if(i<j){//当i和j没有相遇时
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
//将基准数归为
a[left] = a[i];
a[i] = temp;
//递归算法
quickSort(a,left,i-1);//继续左边的处理
quickSort(a,i+1,right);//继续右边的处理
return;
}
}
//快速排序的最差时间复杂度和冒泡排序是一样的
//它的平均时间复杂度为O(NlogN)
注意:i的使用就是寻找小于基准数的数,j的使命就是寻找大于基准数的数
小哼买书
1.先使用快速排序,对数组进行从下到大的排序
2.去掉重复的数字
package ch1;
import java.util.Scanner;
/*
1.先使用快速排序,对数组进行从下到大的排序
2.去掉重复的数字
*/
public class Test04 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入参与调查的同学:");
int n = scanner.nextInt();//输入n个数字
int[] a = new int[n];
System.out.println("请输入所有的图书ISBN号:");
for (int i=0; i< n; i++){
a[i] = scanner.nextInt();
}
scanner.close();
quickSort(a,0,n-1);
System.out.print(a[0]);//输出第一个数
for(int i = 1;i<n;i++){
if (a[i-1]!= a[i]){
System.out.print(" " + a[i]);
}
}
}
//快速排序算法
public static void quickSort(int a[],int start,int end){
if (start > end){
return;
}
int i = start;
int j = end;
int t = a[start];
while(i!=j) {
while (i < j && a[j] >= t) {
j--;
}
while (i < j && a[i] <= t) {
i++;
}
if (i < j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
a[start] = a[i];
a[i] =t;
quickSort(a, start,i-1);
quickSort(a,j+1,end);
return;
}
}
//它的平均时间复杂度为O(NlogN)