鍊錶的氣泡排序 Java

2021-09-13 03:34:26 字數 988 閱讀 5847

鍊錶的氣泡排序:

* 思路一:

* 1、交換兩個節點的值(建議使用此種交換元素方式,簡單易行)

* * 思路二:

* 2、交換兩個節點的指標

**:

package com.my.test.datastructure.linkedlist;

/** * 鍊錶的氣泡排序

* * 思路一:

* 1、交換兩個節點的值

* * 思路二:

* 2、交換兩個節點的指標

* */

public class bubblesortlinkedlist

@override

public string tostring()

return this.value + "->" + this.next.tostring();}}

/*** 交換兩個節點的值

*/public static node bubblesort1(node head, int len)

curnode = curnode.next;}}

return head;

}

/*** 交換兩個節點的指標

*/public static node bubblesort2(node head, int len) else

// 因為需要把prenode指向原來的下個節點,所以此處賦值prenode,prenode後移

prenode = curnode.next;

// 1.4、curnode指向下下個節點

curnode.next = tmpnode;

// 更新prenode & curnode指標

} else }}

return newhead;

} public static void main(string args)

}

更多鍊錶排序

參考:

鍊錶氣泡排序

2011 07 22 21 33 111人閱讀收藏 舉報 任意兩個相鄰節點p q位置互換圖示 假設p1 next指向p,那麼顯然p1 next next就指向q,p1 next next next就指向q的後繼節點,我們用p2儲存 p1 next next指標。即 p2 p1 next next,則...

鍊錶氣泡排序

任意兩個相鄰節點p q位置互換圖示 假設p1 next指向p,那麼顯然p1 next next就指向q,p1 next next next就指向q的後繼節點,我們用p2儲存 p1 next next指標。即 p2 p1 next next,則有 p q 排序前 p1 next p1 next nex...

鍊錶排序 氣泡排序

動態陣列的儲存和排序 需求 c不支援動態陣列,申明的時候,一定要指明動態陣列的大小,不能將陣列的大小設定為未知數,但是很多情況 下,資料量的大小是未知數,或者資料量的大小會隨著問題的變化而變化。解決 使用鍊錶結構來儲存資料 include stdafx.h include stdafx.h incl...