算法

参考书籍:《算法图解》《啊哈!算法》

1.二分查找 时间复杂度 log2(N)

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace list
 8 {
 9     class Program
10     {
11         //二分查找
12         //数组必须有序
13         public static int BinarySearch(List<int> intList, int target)
14         {
15             try
16             {
17                 int? n = null;
18                 int low = 0;
19                 int high = intList.Count - 1;
20 
21                 while (low <= high)
22                 {
23                     int mid = (low + high) / 2;
24                     if (intList[mid] == target)
25                     {
26                         Console.WriteLine("mid="+mid);
27                         return mid;
28                     }
29                         
30                     //low和high +-1是为了防止查找到边界时死循环
31                     else if (intList[mid] > target)
32                     {
33                         high = mid-1;
34                         Console.WriteLine("high=" + high + " mid="+mid);
35                     }
36                         
37                     else
38                     {
39                         low = mid +1;
40                         Console.WriteLine("low=" + low + " mid=" + mid);
41                     }
42                         
43                 }
44 
45                 return n.Value;     //越界后跳转到catch{}
46             }
47 
48             catch
49             {
50                 int? n = null;
51                 Console.WriteLine("Error");
52                 return n.Value;     //返回null
53             }
54             
55         }
56 
57         static void Main(string[] args)
58         {
59             int low = 1;
60             int high = 9;
61 
62             List<int> list = new List<int>();
63             for (int i = low; i < high; i++)
64                 list.Add(i);
65             try
66             {
67                 int a = BinarySearch(list, 10);
68                 int b = list[a];       //a值为null则跳转到catch{}
69                 Console.WriteLine("a.Index=" + a + " , a.Value= " + b);
70             }
71 
72             catch
73             {
74                 Console.WriteLine("Can't find target");
75             }
76 
77             Console.ReadKey();
78         }
79     }
80 }
BinarySearch

 2.选择排序

 1 for (int j=0; j<array.Length-1;j++)
 2             {
 3                 int min = j;
 4                 int temp;
 5                 for (int i = j; i < array.Length; i++)
 6                 {
 7                     if (array[min] > array[i])
 8                     {
 9                         min = i;
10                     }
11                   
12                 }
13                 temp = array[min];
14                 array[min] = array[j];
15                 array[j] = temp;
16             }
Select

 3.冒泡排序

 1 string str = Console.ReadLine();
 2             string[] strArray = str.Split(' ');
 3             int[] numArray = new int[strArray.Length];
 4             for(int i=0;i<strArray.Length;i++)
 5             {
 6                 numArray[i] = Convert.ToInt32(strArray[i]);
 7             }
 8             for(int i=0;i<numArray.Length;i++)
 9             {
10                 for(int j=i+1;j<numArray.Length;j++)
11                 {
12                     if(numArray[j]<numArray[i])
13                     {
14                         int temp = numArray[j];
15                         numArray[j] = numArray[i];
16                         numArray[i] = temp;
17                     }
18                 }
19             }
20 
21            for(int i=0;i<numArray.Length;i++)
22             {
23                 Console.Write(numArray[i] + " ");
24             }
25 
26             Console.ReadKey();
Bubble

 

 1 //当发生交换时继续排序,数组有序时则不交换
 2             int[] index = { 5, 4, 6 };
 3             bool isSwap = true;
 4             do
 5             {
 6                 isSwap = false;
 7                 for (int i = 0; i < index.Length - 1; i++)
 8                 {
 9                     if (index[i] > index[i + 1])
10                     {
11                         int temp = index[i];
12                         index[i] = index[i + 1];
13                         index[i + 1] = temp;
14                         isSwap = true;
15                     }
16                 }
17             } while (isSwap);
另一种

 

 1 //
 2     public class Employee
 3     {
 4         public string Name { get; private set; }
 5         public int Salary { get; private set; }
 6 
 7         public Employee(string name, int salary)
 8         {
 9             this.Name = name;
10             this.Salary = salary;
11         }
12 
13         //比较薪水大小
14         public static bool Compare(Employee e1, Employee e2)
15         {
16             if (e1.Salary > e2.Salary)
17                 return true;
18 
19             return false;
20         }
21         //重写输出
22         public override string ToString()
23         {
24             return Name + ":" + Salary;
25         }
26     }
27 
28 
29 
30 
31 /// <summary>
32         /// 通用的冒泡排序方法
33         /// </summary>
34         /// <typeparam name="T">类型</typeparam>
35         /// <param name="sortArray">数组</param>
36         /// <param name="compareMethod">数组内置的比较方法,返回bool</param>
37         static void CommonSort<T>(T[] sortArray,Func<T,T,bool> compareMethod)
38         {
39             bool isSwap = true;
40             do
41             {
42                 isSwap = false;
43                 for (int i = 0; i < sortArray.Length - 1; i++)
44                 {
45                     if (compareMethod(sortArray[i],sortArray[i+1]))
46                     {
47                         T temp = sortArray[i];
48                         sortArray[i] = sortArray[i + 1];
49                         sortArray[i + 1] = temp;
50                         isSwap = true;
51                     }
52                 }
53             } while (isSwap);
54         }
通用冒泡排序参考格式

 

4.乱序排序

 1 int[] numArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 2             int[] newArray = new int[numArray.Length];
 3             bool[] isAssigned = new bool[numArray.Length];  //判断是否已分配
 4             Random random = new Random();
 5             
 6             for(int i=0;i<numArray.Length;i++)
 7             {
 8                 int n=0;
 9                 bool isFounded= false;  //判断是否已找到
10                 while(isFounded==false)
11                 {
12                     n = random.Next(numArray.Length);
13                     if(isAssigned[n]==false)
14                     {
15                         isFounded = true;
16                     }
17                 }
18                 isAssigned[n] = true;
19                 newArray[i] = numArray[n];
20             }
21 
22 
23             foreach(int n in newArray)
24             {
25                 Console.Write(n + " ");
26             }
27             Console.ReadKey();
Random

或者也可以设置成两个List<>,当生成随机数x后,把x从List中remove

 

5.快排 时间复杂度 n^2 ~ N*log2(N)

 1 #include "pch.h"
 2 #include <iostream>
 3 #include <stdio.h>
 4 
 5 
 6 int a[101], n;
 7 
 8 void quickSort(int left, int right) {
 9     int i, j, t, temp;
10     if (left > right) {
11         return;
12     }
13 
14     temp = a[left];
15     i = left;
16     j = right;
17     while (i != j) {
18         while (a[j] >= temp && i < j) {
19             j--;
20         }
21         while (a[i] <= temp && i < j) {
22             i++;
23         }
24 
25         if (i < j) {
26             t = a[i];
27             a[i] = a[j];
28             a[j] = t;
29         }
30     }
31 
32     a[left] = a[i];
33     a[i] = temp;
34 
35     quickSort(left, i - 1);
36     quickSort(i + 1, right);
37     return;
38 }
39 
40 int main()
41 {
42     int i, j;
43     scanf_s("%d", &n);
44     for (i = 1; i <= n; ++i) {
45         scanf_s("%d", &a[i]);
46     }
47 
48     quickSort(1, n);
49 
50     for (i = 1; i <= n; ++i) {
51         printf("%d", a[i]);
52     }
53 
54     getchar();
55     getchar();
56     return 0;
57 }
选择

 

6.队列

 1 #include "pch.h"
 2 #include <iostream>
 3 #include <stdio.h>
 4 
 5 struct queue 
 6 {
 7     int data[101];
 8     int head;
 9     int tail;
10 };
11 
12 int main()
13 {
14     queue q;
15     int i;
16     q.head = 0;
17     q.tail = 0;
18     for (i = 0; i <= 8; i++) {
19         scanf_s("%d", &q.data[q.tail]);
20         q.tail++;
21     }
22 
23     while (q.head<q.tail)
24     {
25         printf("%d", q.data[q.head]);
26         q.head++;
27 
28         q.data[q.tail] = q.data[q.head];
29         q.tail++;
30         q.head++;
31     }
32 
33     getchar();
34     getchar();
35 
36     return 0;
37 }
队列

 

7.栈 例判断是否为回文

 1 #include "pch.h"
 2 #include <iostream>
 3 #include <stdio.h>
 4 #include <string.h>
 5 
 6 int main()
 7 {
 8     char a[101], s[101];
 9     int i, len, mid, next, top;
10 
11     gets_s(a);
12     len = strlen(a);
13     mid = len / 2 - 1;
14 
15     top = 0;
16     for (i = 0; i <= mid; ++i) {
17         s[++top] = a[i];
18     }
19 
20     if (len % 2 == 0) {
21         next = mid + 1;
22     }
23     else
24     {
25         next = mid + 2;
26     }
27 
28     for (i = next; i <= len - 1; ++i) {
29         if (a[i] != s[top]) {
30             break;
31         }
32         top--;
33     }
34 
35     if (top == 0) {
36         printf("YES");
37     }
38     else
39     {
40         printf("NO");
41     }
42 
43     getchar();
44     getchar();
45     return 0;
46 }

 

posted @ 2018-06-22 14:54  QQW的进化之旅  阅读(177)  评论(0编辑  收藏  举报