數(shù)組

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace ConsoleApp4

{

? ? class Program

? ? {

? ? ? ? static void Main(string[] args)

? ? ? ? {


二分查找 條件 數(shù)組必須排序好的數(shù)組

使用遞歸進(jìn)行二分查找 取得數(shù)組中間索引的值判斷要查找的值

如果大于往前查找反之往后查找

1,從小到大拍好序的數(shù)組2,開始索引,結(jié)束索引,要查找的值

static int BinarySearch(int[] arr, int low, int high, int key) {

? ? ? ? ? ? int mid = (low + high) / 2;

? ? ? ? ? ? if (low > high) return -1;

? ? ? ? ? ? else {

? ? ? ? ? ? ? ? if (arr[mid] == key) return mid;

? ? ? ? ? ? ? ? else if (arr[mid] > key)

? ? ? ? ? ? ? ? ? ? return BinarySearch(arr, low, mid - 1, key);

? ? ? ? ? ? ? ? else? {


? ? ? ? ? ? ? ? ? ? return BinarySearch(arr,mid + 1,high, key);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

static int Search(int[] arr, int key) {

? ? ? ? ? ? int low = 0;

? ? ? ? ? ? int length = arr.Length - 1;

? ? ? ? ? ? int mid = (low + length) / 2;

? ? ? ? ? ? while (length>=low)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? if (arr[mid] == key) return mid;

? ? ? ? ? ? ? ? else if (arr[mid] > key)

? ? ? ? ? ? ? ? ? ? mid -= 1;

? ? ? ? ? ? ? ? else mid++;


? ? ? ? ? ? }

? ? ? ? ? ? return -1;

? ? ? ? }










(Fibonacci)斐波拉切數(shù)列

Fibonacci數(shù)列是按以下順序排列的數(shù)字:

1,1,2,3,5,8,13,21,34,55....規(guī)律后一個(gè)數(shù)加上前一個(gè)數(shù)等于第三個(gè)

int a = 1;

? ? ? ? ? ? int b = 1;

? ? ? ? ? ? for (int i = 2; i < 15; i++)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? b = a + b;

? ? ? ? ? ? ? ? a = b - a;

? ? ? ? ? ? ? ? Console.WriteLine(a);

? ? ? ? ? ? }

int[] a = new int[10];

? ? ? ? ? ? a[0] = 1;

? ? ? ? ? ? a[1] = 1;

? ? ? ? ? ? for (int i = 2; i < a.Length; i++)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? a[i] = a[i - 1] + a[i - 2];

? ? ? ? ? ? }

? ? ? ? ? ? for (int i = 0; i < a.Length; i++)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? Console.WriteLine(a[i]);

? ? ? ? ? ? }



----------插入排序

for (int i = 0; i < array.Length; i++)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? int t = array[i];

? ? ? ? ? ? ? ? int j = i;

? ? ? ? ? ? ? ? while (j>0&&(array[j-1]>t))

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? array[j] = array[j - 1];

? ? ? ? ? ? ? ? ? ? j--;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? array[j] = t;

? ? ? ? ? ? }



? ? ? ? ? ? //數(shù)組常見操作

? ? ? ? ? ? //獲取最大值,最小值

? ? ? ? ? ? //排序(選擇排序,冒泡排序)

? ? ? ? ? ? //折半查找(二分查找)

? ? ? ? ? ? int[] array = new int[] { 10, 9, 11, 6, 26, 23, 1, 54, 23, };

? ? ? ? ? ? int temp ;

? ? ? ? ? ? //選擇排序

? ? ? ? ? ? for (int i = 0; i < array.Length; i++)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? for (int j = i+1; j < array.Length; j++)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? if (array[i] < array[j]) {

? ? ? ? ? ? ? ? ? ? ? ? temp = array[i];

? ? ? ? ? ? ? ? ? ? ? ? array[i] = array[j];

? ? ? ? ? ? ? ? ? ? ? ? array[j] = temp;

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? for (int i = 0; i < array.Length; i++)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? Console.WriteLine(array[i]);

? ? ? ? ? ? }

? ? ? ? ? ? Console.WriteLine("--------------------------------------------------------------");

? ? ? ? ? ? //冒泡排序

? ? ? ? ? ? int[] Bubble = new int[] { 15, 15, 487, 1512, 12, 20, 0, 4154, 121, 95, 124 ,4854};

? ? ? ? ? ? for (int i = 0; i < Bubble.Length-1; i++)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? for (int j = 0; j < Bubble.Length-i-1; j++)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? if (Bubble[j] > Bubble[j + 1]) {

? ? ? ? ? ? ? ? ? ? ? ? temp = Bubble[j + 1];

? ? ? ? ? ? ? ? ? ? ? ? Bubble[j + 1] = Bubble[j];

? ? ? ? ? ? ? ? ? ? ? ? Bubble[j] = temp;

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? for (int f = 0; f < Bubble.Length; f++)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? Console.WriteLine(Bubble[f]);

? ? ? ? ? ? }

? ? ? ? ? ? Console.WriteLine("_______________________________________________________________________________");

? ? ? ? ? ? //折半查找

? ? ? ? ? ? int min = 0;

? ? ? ? ? ? int max = Bubble.Length-1;

? ? ? ? ? ? int mid = (min + max) /2;

? ? ? ? ? ? while (min<=max)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? mid = (min + max) / 2;

? ? ? ? ? ? ? ? if (4854 < Bubble[mid])

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? max = mid - 1;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? else if (4854 > Bubble[mid])

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? min = mid + 1;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? else {

? ? ? ? ? ? ? ? ? ? Console.WriteLine(mid);

? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? Console.ReadKey();

? ? ? ? }

? ? }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容