單鏈表插入排序

2021-09-01 16:30:10 字數 1127 閱讀 5845

鍊錶插入排序的基本思想是:

在每個物件的結點中增加乙個鏈域link。

對於存放於陣列中的一組物件v[1],v[2],…,v[n],若v[1],v[2],…,v[i-1]已經通過鏈結指標link,按其排序碼的大小,從小到大鏈結起來

現在要插入v[i],i=2,3,…,n,則必須在前面i-1個鏈結起來的物件當中,循鏈順序檢測比較,找到v[i]應插入(鏈入)的位置,把v[i]插入,並修改相應的鏈結指標

這樣就可得到v[1],v[2],…,v[i]的乙個通過鏈結指標排列好的鍊錶。如此重複執行,直到把v[n]也插入到鍊錶中排好序為止。

public class listinsertionsort  else 

// when curr=-1)

link[next] = i;

link[i] = curr;

}} // step 2: arrange v by link information: find curr and swap it with

// i

curr = head; // to scan linked list, always start with head

// note the difference:

// in this step: between iterations, curr doesn't start with head at the

// beginning

// the link creation step: between iterations, we always scan from

// curr=head to find the insert position

for (int i = 0; i < n; i++)

curr = next; // next iteration we start from 'next'

} // link is now useless, let it be garbage collected.

} public static void main(string args) ;

listinsertionsort.sort(v);

for (int key : v)

}}

單鏈表插入排序

單鏈表插入排序演算法基本思想是構建乙個新的有序煉表頭結點,並將原來的鍊錶節點依次插入到新建的有序鍊錶中。如下 include stdafx.h include using namespace std struct listnode class solution listnode tmpnext pt...

單鏈表插入排序

include include typedef int elemtype 元素的資料型別 typedef struct lnode linknode 單鏈表結點型別 尾插法建立單鏈表,細節不表 void createlistr linknode l,elemtype a,int n 輸出線性表,細節...

單鏈表的插入排序

解析 若原單鏈表有乙個或乙個以上的資料結點,首先構造只含乙個資料結點的有序表 只含乙個資料結點的單鏈表一定是有序的 掃瞄原單鏈表餘下的結點 p 直到p null 為止,在有序表中通過比較找插入 p的前驅結點 q,然後將 p插入到 q之後 這裡實際上採用的是直接插入法排序方法 對應的演算法如下 1 2...