冒泡排序与二分查找
今天闲着无聊自己写点程序玩,可以实现的效果是:先提示用户输入一个未排序的数组,然后把排完序后的数组输出,再提示用户输入要查找的元素,然后返回索引。这个程序可以一直进行下去,直到用户输入0.
C++实现:
1 #include <iostream>
2 #include <string.h>
3
4 using namespace std;
5 int i,j;
6
7 void swap(int* a,int* b)
8 {
9 int temp;
10 temp = *a;
11 *a = *b;
12 *b = temp;
13 }
14
15 void bubbleSort(int array[],int n)
16 {
17 for (i = n-1; i >= 0; i--)
18 {
19 for (j = 0; j < i; j++)
20 {
21 if (array[j] > array[j + 1]) {
22 swap(&array[j], &array[j + 1]);
23 }
24 }
25 }
26 }
27
28 int binarySearch(int arr[],int n,int target)
29 {
30 int begin = 0;
31 int end = n - 1;
32 while (begin <= end)
33 {
34 int indexOfMid = begin + ((end - begin) / 2);
35 int mid = arr[indexOfMid];
36 if (target < mid) {
37 end = indexOfMid - 1;
38 }
39 else if (target > mid) {
40 begin = indexOfMid + 1;
41 }
42 else {
43 return indexOfMid;
44 }
45 }
46 return -1;
47 }
48
49 int main(void)
50 {
51 int data[100];
52 int t;
53 cout << "输入0结束程序。" << endl;
54 cout << "请输入你想要排序的数组的长度:";
55
56 while (cin >> t)
57 {
58 if (t == 0) break;
59 cout << "请输入数组的所有元素:";
60 for (i = 0; i < t; i++)
61 {
62 cin >> data[i];
63 }
64 bubbleSort(data, t);
65 cout << "排完序后的数组是:";
66 for (i = 0; i < t; i++)
67 {
68 cout << data[i] << " ";
69 }
70 cout << endl;
71
72 cout << "请输入你想要查找的元素:";
73 int index;
74 cin >> index;
75 int res = binarySearch(data, t, index);
76 cout << "你想要查找的元素在排完序后的数组中的索引是:";
77 cout << res << endl;
78 cout << "请输入你想要排序的数组的长度:";
79 }
80 return 0;
81 }

Java实现:
1 import java.util.Scanner;
2
3 public class SearchDemo {
4 public static void main(String[] args) {
5 Scanner s = new Scanner(System.in);
6 int[] data = null; //声明数组后,不立即初始化,就给个空值
7 System.out.println("输入0结束程序。");
8 System.out.print("请输入你想要排序的数组的长度:");
9 while(s.hasNext())
10 {
11 int len = s.nextInt(); //数组长度
12 if(len == 0) break;
13 data = new int[len]; //给定该数组的长度
14 System.out.print("请输入数组的所有元素:");
15 for(int i = 0;i < len;i++)
16 {
17 data[i] = s.nextInt(); //数组的初始化
18 }
19
20 bubbleSort(data);
21 System.out.print("排完序后的数组是:");
22 for(int k = 0;k < len;k++)
23 {
24 System.out.print(data[k]+" ");
25 }
26
27 System.out.println();
28
29 System.out.print("请输入你想要查找的元素:");
30 int index = s.nextInt();
31 int res = binarySearch(data, index);
32 System.out.print("你要搜寻的元素在排完序后的数组中的索引是:");
33 System.out.println(res);
34 System.out.print("请输入你想要排序的数组的长度:");
35 }
36 }
37
38 public static void bubbleSort(int[] array){ //冒泡排序
39 for(int i = array.length - 1;i >= 0;i--)
40 {
41 for(int j = 0;j < i;j++)
42 {
43 if(array[j] > array[j+1]){
44 int temp = array[j];
45 array[j] = array[j+1];
46 array[j+1] = temp;
47 }
48 }
49 }
50 }
51
52 public static int binarySearch(int[] array,int target){ //二分查找
53 int begin = 0;
54 int end = array.length - 1;
55 while(begin <= end)
56 {
57 int indexOfMid = begin + ((end-begin)/2);
58 int mid = array[indexOfMid];
59 if(target > mid){
60 begin = indexOfMid + 1;
61 }else if(target < mid){
62 end = indexOfMid - 1;
63 }else{
64 return indexOfMid;
65 }
66 }
67 return -1;
68 }
69 }


浙公网安备 33010602011771号