1、冒泡排序
冒泡排序算法的原理如下:
比较相邻的元素。如果第一个比第二个大,就交换他们两个;
对每一对相邻元素做同样的工作,从开始第一对到结尾的最后一对。最后的元素是最大的数;
针对所有的元素重复以上的步骤,除了最后一个;
持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。
public class 冒泡排序 { public static void main(String[] args) { int[] a = { 7, 15, 24, 3, 19, 6 }; // 第一躺 // for(int i=0;i<a.length-1;i++) { // if(a[i]>a[i+1]) { // int temp = a[i]; // a[i] = a[i+1]; // a[i+1] = temp; // } // // } // // //第二趟 // for(int i=0;i<a.length-2;i++) { // if(a[i]>a[i+1]) { // int temp = a[i]; // a[i] = a[i+1]; // a[i+1] = temp; // } // } // System.out.println(); // //第三趟 // for(int i=0;i<a.length-3;i++) { // if(a[i]>a[i+1]) { // int temp = a[i]; // a[i] = a[i+1]; // a[i+1] = temp; // } // } // // 第四趟 // for(int i=0;i<a.length-4;i++) { // if(a[i]>a[i+1]) { // int temp = a[i]; // a[i] = a[i+1]; // a[i+1] = temp; // } // } // 第五趟 /* * for(int i=0;i<a.length-5;i++) { if(a[i]>a[i+1]) { int temp = a[i]; a[i] = * a[i+1]; a[i+1] = temp; } } */ for (int k = 1; k <= 5; k++) { // 比较次数 for (int i = 0; i < a.length - k; i++) { if (a[i] > a[i + 1]) { // 交换 int temp = a[i]; a[i] = a[i + 1]; a[i + 1] = temp; } } } for (int i = 0; i < a.length; i++) { System.out.println(a[i] + " "); } } }
2、选择排序
选择排序(Selection sort)是一种简单直观的排序算法。它的工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到全部待排序的数据元素排完。 选择排序是不稳定的排序方法。
public class 选择排序1 { public static void main(String[] args) { int[] a = { 20, 33, 14, 5, 61 }; // int j=0;//待放置的索引位置 for (int j = 0; j < a.length - 1; j++) { int min = j; // 存放查找出的最小的索引 for (int i = a.length - 1; i > j; i--) { if (a[i] < a[min]) { min = i; } } // 交换a[j] ==>a[min]的位置 int temp = a[j]; a[j] = a[min]; a[min] = temp; } for(int i=0;i<a.length;i++) { System.out.println(a[i] + " "); } } }3、插入排序
插入排序(Insertion sort)是一种简单直观且稳定的排序算法。如果有一个已经有序的数据序列,要求在这个已经排好的数据序列中插入一个数,但要求插入后此数据序列仍然有序,这个时候就要用到一种新的排序方法——插入排序,插入排序的基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一个新的、个数加一的有序数据,算法适用于少量数据的排序,时间复杂度为为O(n^2)。是稳定的排序方法。插入算法把要排序的数组分成两部分:第一部分包含了这个数组的所有元素,但将最后一个元素除外(让数组多一个空间才有插入的位置),而第二部分就只包含这一个元素(即待插入元素)。在第一部分排序完成后,再将这个最后元素插入到已排好序的第一部分中。
插入排序的基本思想是:每步将一个待排序的记录,按其关键码值的大小插入前面已经排序的文件中适当位置上,直到全部插入完为止。
public class 插入排序 { public static void main(String[] args) { int[] a = {20,33,5,64,7}; //第一趟 for(int index =0;index <a.length-1;index++) { //int index =0;//前面(待插入)数组最后一个元素的索引 int temp=a[index+1]; //带插入元素值 int insert_index = index +1; //插入位置的索引 //寻找待插入位置的索引 for(int i= index;i>=0;i--) { if(temp <a[i]) { insert_index =i; }else { break; } } //插入 //把插入之后的元素向后移动一个位置 for(int i = index;i>=insert_index;i--){ a[i+1] = a[i]; // 后移,腾出插入元素所需的位置 } a[insert_index] = temp; /* System.out.println("----------------------------------"); index =1;//前面(带插入)数组最后一个元素的索引 //第二趟 temp=index+1; //带插入元素值 insert_index = index +1; //插入位置的索引 //寻找待插入位置的索引 for(int i= index;i>=0;i--) { if(temp <a[i]) { insert_index =i; }else { break; } } //插入 //int tmp = a[temp]; //把插入之后的元素向后移动一个位置 for(int i = index;i>=insert_index;i--){ a[i+1] = a[i]; } a[insert_index] = temp; }*/ } for(int i =0;i<a.length;i++) { System.out.print(a[i] + " "); } /*int[] a1 = {20,33,5,64,7}; for(int index =0;index <a1.length-1;index++) { int temp=a1[index+1]; //带插入元素值 int insert_index = index +1; for(int i= index;i>=0;i--) { if(temp <a1[i]) { insert_index =i; }else { break; } } for(int i = index;i>=insert_index;i--){ a1[i+1] = a1[i]; // 后移,腾出插入元素所需的位置 } a[insert_index] = temp; for(int i =0;i<a1.length;i++) { System.out.print(a[i] + " "); } }*/ } }
4、顺序查找
顺序查找是按照序列原有顺序对数组进行遍历比较查询的基本查找算法。
基本原理:对于任意一个序列以及一个给定的元素,将给定元素与序列中元素依次比较,直到找出与给定关键字相同的元素,或者将序列中的元素与其都比较完为止。
import java.util.Scanner; public class 顺序查找 { /* * 冒泡排序 排好的数组 */ public static void main(String[] args) { int[] a = { 20, 33, 12, 88, 76 }; boolean flag = true; Scanner s = new Scanner(System.in); int t = s.nextInt(); for (int i = 0; i < a.length - 1; i++) { if (a[i] == t) { System.out.println("找到"); flag = false; break; } } if (flag) { System.out.println("未找到"); } for (int i = 0; i < a.length; i++) { System.out.println(a[i] + " "); } } }5、二分折半查找
二分查找也称折半查找(Binary Search),它是一种效率较高的查找方法。但是,折半查找要求线性表必须采用顺序存储结构,而且表中元素按关键字有序排列。
算法要求:
1.必须采用顺序存储结构;
2.必须按关键字大小有序排列。
查找过程:
首先,假设表中元素是按升序排列,将表中间位置记录的关键字与查找关键字比较,如果两者相等,则查找成功;否则利用中间位置记录将表分成前、后两个子表,如果中间位置记录的关键字大于查找关键字,则进一步查找前一子表,否则进一步查找后一子表。重复以上过程,直到找到满足条件的记录,使查找成功,或直到子表不存在为止,此时查找不成功。
import java.util.Scanner; public class 二分折半查找 { //定义好冒泡排序 二分查找的前提时排序好的数组 public static int[] bubble(int[] args) { for (int k = 1; k <= args.length - 1; k++) { // 比较次数 for (int i = 0; i < args.length - k; i++) { if (args[i] > args[i + 1]) { // 交换 int temp = args[i]; args[i] = args[i + 1]; args[i + 1] = temp; } } } return args; } // public static void bubble(int[] args) { // // for (int k = 1; k <= args.length - 1; k++) { // 比较次数 // for (int i = 0; i < args.length - k; i++) { // if (args[i] > args[i + 1]) { // 交换 // int temp = args[i]; // args[i] = args[i + 1]; // args[i + 1] = temp; // } // } // } // // } public static void main(String[] args) { int[] a = {20,33,12,88,76,23}; a = bubble(a); for(;;) { Scanner s = new Scanner(System.in); int t = s.nextInt(); int f = 0; int l = a.length-1; int m=0; while(true) { m = (l+f)/2; if(t ==a[m]) { System.out.println("找到"); break; }else { if(t>a[m]) { f = m+1; }else { l = m-1; } } if(f>l) { System.out.println("未找到"); break; } } } } }
