修改插入排序,將順序查詢改為二分查詢
#include<iostream> using namespace std ; void InsertSort(int *a,int n){ int r,l,m,temp; for(int i=1;i<n;i++){ temp=a[i]; l=0; r=i-1; while(l<=r){ m=(l+r)>>1; if(a[m]<temp) l=m+1; else r=m-1; } for(int j=i-1;j>=l;j--) a[j+1]=a[j]; a[l]=temp; } for(int i=0;i<n;i++) cout << a[i] << " " ; } int main() { int a[9]={9,3,2,5,4,1,7,4,14}; InsertSort(a,9); return 0; }
相關推薦
修改插入排序,將順序查詢改為二分查詢
#include<iostream> using namespace std ; void InsertSort(int *a,int n){ int r,l,m,temp; for(int i=1;i<n;i++){ temp=a[i]; l=0; r=i-1;
windows+ubuntu雙系統,將ubuntu引導改為windows引導
實驗室的電腦,之前系統是windows server,然後我在上面裝了ubuntu,變成了雙系統,但是好像是和nvidia的顯示卡不相容的原因,老是跳不出系統選擇介面就直接進來ubuntu。上週偶然因為ubuntu系統的nvidia驅動被我不小心解除安裝了(貌似是這樣),我裝
插入排序:將一部分看成有序,一部分無序,無序的第一個數字與前面的交換,需要臨時變數temp來儲存無序的第一個
package com.interview.datastructure; public class SelectSort { public static void sort(int[] a) { if (a != null) { for (int i = 1; i
順序表儲存實現氣泡排序,選擇排序,插入排序,希爾排序,基數排序
#include<iostream> using namespace std; const int MAXSIZE = 100; typedef int ElemType; struct Data { ElemType key; // int shu
優化的直接插入排序(二分查詢插入排序,希爾排序)
本博文向大家介紹了插入排序的三種實現:直接插入排序,二分查詢插入排序,希爾排序。詳細分析的其實現過程、時間複雜度和空間複雜度、穩定性以及優化改進策略。最後簡單的做了下效能測試。 直接插入排序 (一)概念及實現 直接插入排序的原理:先將原序列分為有序區和無序區,然後再經過比較和後移操作將無序
python中實現二分查詢,插入排序,歸併排序,快速排序
轉載請註明出處http://blog.csdn.net/feimengjuan/article/details/46400303 1、二分查詢 #二分查詢 def BinarySearch(A,b
經典排序演算法,氣泡排序,選擇排序,直接插入排序,希爾排序,快速排序,歸併排序,二分查詢。原理及python實現。
1.氣泡排序 氣泡排序 1.比較相鄰的元素,如果第一個比第二個大(升序),就交換他們兩個 2.對每一對相鄰的元素做同樣的工作,從開始到結尾的最後一對 這步做完後,最後的元素會是最大的數 3.針對所有的元素重複以上的步驟,除了最
插入排序,歸併排序,快速排序的實現和速度比較(包含二分法查詢所有匹配元素)
最近在學習排序演算法,實現後比較了花費時間情況,現在總結一下插入排序的時間複雜度是O(n²),是一種很直觀的排序方式。歸併排序為O(nlogn),實現起來也比較簡單。快速排序平均時間複雜度也是O(nlogn),實現起來比歸併複雜一些。經過比較發現快速排序比歸併排序要快一些,大
如何修改vs2008修改編譯選項,將/MD或/MDd 改為 /MT或/MTd
當需要解決exe不能在其他電腦執行時,有一種解決方案時修改編譯選項把MD或MDd改為MT或MTD,那麼VS2008這個編譯選項的位置 在哪裡。看下圖。進入工程屬性(這個應該知道在哪吧)另一種就是決絕exe不能在其他電腦執行的方法是,設定工程為靜態編譯(靜態庫)
合並排序,將兩個已經排序的數組合並成一個數組
n) ring 一個數 合並排序 sizeof 並排 names 排序 name #include<iostream> #include<string.h> using namespace std; void MergeArray(int a[]
Java選擇排序,插入排序,快速排序
col log println 左移 i++ void -1 left oid public class Test { public static void main(String[] args) { int a[] = { 1, 2, 3,
c程序設計 8.15寫幾個函數:①輸個職工的姓名和職工號;②按職工號由小到大順序排序,姓名順序也隨之調整;③要求輸入一個職工號,用折半法找出該職工的姓名,從主函數輸入要查找的職工號,輸出該職工
xmx ebr ckey ros lbp loj ase vfk cu2 8.15寫幾個函數:①輸個職工的姓名和職工號;②按職工號由小到大順序排序,姓名順序也隨之調整;③要求輸入一個職工號,用折半法找出該職工的姓名, 從主函數輸入要查找的職工號,輸出該職工。 寫的時候為
Centos7修改網卡,把ifcfg-eno16777736改成eth0的操作步驟
centos7 ifcfg-eno16777736 1、修改前我的網卡信息是這樣的:2、打開網卡配置文件,把配置文件中的NAME參數改成eth0命令:【[email protected] etc】# vi /etc/sysconfig/network-scripts/ifcfg-eno16
插入排序,python
sel ret div 練手 previous self obj spa col class insertSort: def insert(self,arr): for i in range(1,len(arr)): for
排序算法(冒泡排序,選擇排序,插入排序,快速排序)
str div cell dex clas 基準 exchange quick partition 數組的排序算法 選擇排序 每次選擇所要排序得數組中的最大值(由大到小排序,由小到大排序則選擇最小值)的數組元素,將這個數組元組的值與最前面沒有排序的數組元素進行交換,
python排序(插入排序) 從小到大順序
range def brush lis pop break pri highlight python def insert_sort(ilist): for i in range(len(ilist)): for j in range(i):
排序演算法:選擇排序,直接插入排序,氣泡排序
package com.Algorithm.Search_Sort; import java.util.Arrays; public class Sort { public int array[] = {99,34,76,92,34,17,77,41,40,36,6}; //
python: 選擇排序,氣泡排序,插入排序,快速排序
def selectSort(lyst): n = len(lyst) for i in range(n - 1): minindex = i for j in range(i + 1, n): if lyst[j] <
排序上篇(選擇排序,氣泡排序,插入排序,希爾排序)
1.選擇排序 (1)原理: 在要排序的一組數中,用第一個數與後面的數依次進行判斷,若大於後面的則進行交換;然後依次再用第二個數與後面的數進行交換,如此迴圈到倒數第二個數和最後一個數比較為止。 (2)圖解: 內層第一次迴圈如下: 外層控制迴圈次數:
簡單排序——氣泡排序,插入排序,逆序對
1、前提 void X_Sort ( ElementType A[], int N )//A是一個數組,N是陣列的個數。 大多數情況下,為簡單起見,討論從小大的整數排序 N是正整數 只討論基於比較的排序(> = < 有定義) 只討論內部