# 链表排序（冒泡、选择、插入、快排、归并、希尔、堆排序）

struct ListNode {

int val;

ListNode *next;

ListNode(int x) : val(x), next(NULL) {}

};

class Solution {
public:
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
while(p != NULL)
{
ListNode *tmp = pstart->next, *pre = pstart;
while(tmp != p && p->val >= tmp->val) //找到插入位置
{tmp = tmp->next; pre = pre->next;}
if(tmp == p)pend = p;
else
{
pend->next = p->next;
p->next = tmp;
pre->next = p;
}
p = pend->next;
}
delete pstart;
}
};

class Solution {
public:
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
//选择排序
ListNode *pstart = new ListNode(0);
ListNode*sortedTail = pstart;//指向已排好序的部分的尾部

while(sortedTail->next != NULL)
{
ListNode*minNode = sortedTail->next, *p = sortedTail->next->next;
//寻找未排序部分的最小节点
while(p != NULL)
{
if(p->val < minNode->val)
minNode = p;
p = p->next;
}
swap(minNode->val, sortedTail->next->val);
sortedTail = sortedTail->next;
}

delete pstart;
}
};

void quicksort(vector<int>&arr, int low, int high)

{

  if(low < high)

  {

   int middle = mypartition(arr, low, high);

   quicksort(arr, low, middle-1);

   quicksort(arr, middle+1, high);

  }

}

class Solution {
public:
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
//链表快速排序
}
{
//链表范围是[low, high)
{
qsortList(mid->next, tail);
}
}
ListNode* partitionList(ListNode*low, ListNode*high)
{
//链表范围是[low, high)
int key = low->val;
ListNode* loc = low;
for(ListNode*i = low->next; i != high; i = i->next)
if(i->val < key)
{
loc = loc->next;
swap(i->val, loc->val);
}
swap(loc->val, low->val);
return loc;
}
};

class Solution {
public:
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
//链表快速排序
}
{
//链表范围是[low, high)
{
qsortList(mid, mid->next, tail);
}
}
ListNode* partitionList(ListNode* lowPre, ListNode* low, ListNode* high)
{
//链表范围是[low, high)
int key = low->val;
ListNode node1(0), node2(0);//比key小的链的头结点，比key大的链的头结点
ListNode* little = &node1, *big = &node2;
for(ListNode*i = low->next; i != high; i = i->next)
if(i->val < key)
{
little->next = i;
little = i;
}
else
{
big->next = i;
big = i;
}
big->next = high;//保证子链表[low,high)和后面的部分连接
little->next = low;
low->next = node2.next;
lowPre->next = node1.next;//为了保证子链表[low,high)和前面的部分连接
return low;
}
};

class Solution {
public:
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
//链表归并排序
else
{
//快慢指针找到中间节点
while(fast->next != NULL && fast->next->next != NULL)
{
fast = fast->next->next;
slow = slow->next;
}
fast = slow;
slow = slow->next;
fast->next = NULL;
slow = sortList(slow);//后半段排序
return merge(fast,slow);
}

}
// merge two sorted list to one
{
ListNode *res , *p ;
p = res;

{
{
}
else
{
}
p = p->next;
}
return res;
}
};

class Solution {
public:
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
//链表快速排序
ListNode *p = NULL;
bool isChange = true;
{
isChange = false;//标志当前这一轮中又没有发生元素交换，如果没有则表示数组已经有序
for(; q->next && q->next != p; q = q->next)
{
if(q->val > q->next->val)
{
swap(q->val, q->next->val);
isChange = true;
}
}
p = q;
}
}
};

【版权声明】转载请注明出处：http://www.cnblogs.com/TenosDoIt/p/3666585.html

posted @ 2014-04-15 16:34  tenos  阅读(71473)  评论(4编辑  收藏  举报