C 實現模板順序表和三種排序方法

2021-05-25 19:43:51 字數 1035 閱讀 6058

template

class sequentiallist

;template

sequentiallist::sequentiallist(void)

template

sequentiallist::~sequentiallist(void)

}template

void sequentiallist::add(t item)

else

delete m_pbuf;

temp[m_count] = item;

m_pbuf = temp;

temp = null;

m_buflength+=m_increaslength;

}m_count++;

}template

void sequentiallist::display()}}

}//快排演算法

template

int sequentiallist::sort1(int m_low,int m_high)

m_pbuf[low] = m_pbuf[high];

while((low < high)&&(m_pbuf[low] < pivotkey))

m_pbuf[high] = m_pbuf[low];

}m_pbuf[low] = pivotkey;

return low;

}//遞迴呼叫快排演算法

template

void sequentiallist::sort2(int m_low,int m_high)

}//對類內元素進行排序

template

void sequentiallist::qsort()

//選擇排序

template

void sequentiallist::selectsort()

}t temp2 = m_pbuf[i];

m_pbuf[i] = m_pbuf[m_number];

m_pbuf[m_number] = temp2;}}

氣泡排序三種實現方法

氣泡排序是非常容易理解和實現,以從小到大排序舉例 設陣列長度為n。1 比較相鄰的前後二個資料,如果前面資料大於後面的資料,就將二個資料交換。2 這樣對陣列的第 0個資料到 n 1個資料進行一次遍歷後,最大的乙個資料就 沉 到陣列第 n 1個位置。3 n n 1 如果n不為0 就重複前面二步,否則排序...

三種排序方法

氣泡排序 原理 對乙個數列,我們將它進行輪循和交換,每次輪循出最大數或最小數放在對尾,依次進行迴圈,輪循長度為 1。int num new int for int i 0 i 1 i 插入排序 原理 對乙個數列,我們從第二個數開始,將它與它前面的數字進行比較,每次選出最大 或最小的數放在隊首,因而形...

排序 常見三種 C 實現

簡單總結下堆排序的基本思路 a.將無序序列構建成乙個堆,根據公升序降序需求選擇大頂堆或小頂堆 b.將堆頂元素與末尾元素交換,將最大元素 沉 到陣列末端 c.重新調整結構,使其滿足堆定義,然後繼續交換堆頂元素與當前末尾元素,反覆執行調整 交換步驟,直到整個序列有序。其中建堆的時間複雜度為o n 而排序...