/**
* Sort a linked list using insertion sort.
二重循环
*/
//
// InsertionSortList.c
// Algorithms
//
// Created by TTc on 15/6/22.
// Copyright (c) 2015年 TTc. All rights reserved.
//
/**
* Sort a linked list using insertion sort.
二重循环
*/
#include "InsertionSortList.h"
#include <stdbool.h>
#include <stdlib.h>
#include "List.h"
/********************************************************************/
// LeetCode
/********************************************************************/
struct ListNode {
int val;
struct ListNode *next;
};
static void
Swap(int *x, int *y){
int tmp = *x;
*x = *y;
*y = tmp;
}
//O(n^2)
struct ListNode*
insertionSortList(struct ListNode* head) {
if(head == NULL) return head;
struct ListNode* iter = head->next;
struct ListNode* tmp = NULL;
while(iter != NULL){
tmp = head;
while(tmp->val < iter->val && tmp != iter) tmp = tmp->next;
if(tmp != iter){
while(tmp != iter){
Swap(&(tmp->val), &(iter->val));
tmp = tmp->next;
}
}
iter = iter->next;
}
return head;
}
//O(n^2)
struct ListNode*
insertionSortList01(struct ListNode* head) {
if (!head || !head->next) return head;
struct ListNode* front = (struct ListNode*)malloc(sizeof(*front));
front->next = head;
head = front; // insert head node
struct ListNode* pos = head->next;
while (pos->next){
struct ListNode* prev = pos->next;
if (prev->val < pos->val){
struct ListNode* loop_pos = head;
while (prev->val >= loop_pos->next->val) loop_pos = loop_pos->next;
pos->next = prev->next;
prev->next = loop_pos->next;
loop_pos->next = prev;
continue;
}
pos = prev;
}
return front->next;
}
/********************************************************************/
/********************************************************************/
/********************************************************************/
// List.c 是范性类 单链表
/********************************************************************/
//范型法
static void
swap_void(void *a,void *b,size_t size){
unsigned char *p1=(unsigned char *)a; //强制类型转换
unsigned char *p2=(unsigned char *)b;
unsigned char temp; //字节型的嫁衣
while(size--){
temp=*p1;
*p1=*p2;
*p2=temp;
p1++;
p2++;
}
}
ListElmt*
tt_insertionSortList(ListElmt* head) {
if (!head || !head->next) return head;
ListElmt* front = (ListElmt*)malloc(sizeof(*front));
front->next = head;
head = front; // insert head node
ListElmt* pos = head->next;
while (pos->next){
ListElmt* prev = pos->next;
if (*(int*)(prev->data) < *(int*)(pos->data)){
ListElmt* loop_pos = head;
while (*(int*)(prev->data) >= *(int*)(loop_pos->next->data)) loop_pos = loop_pos->next;
pos->next = prev->next;
prev->next = loop_pos->next;
loop_pos->next = prev;
continue;
}
pos = prev;
}
return front->next;
}
void
test_tt_insertionSortList(){
List l1;
list_init(&l1, free);
int *data ;
int array[15] = {10,9,8,100,6,5,4,3,2,1};
for (int i = 0; i< 10; i++) {
if ((data = (int *)malloc(sizeof(int))) == NULL)
return ;
*data = array[i];
if (list_ins_next(&l1, NULL, data) != 0) //逐个插入元素
return;
}
print_list(&l1);
ListElmt* result = tt_insertionSortList(list_head(&l1));
printf("反转后*****************\n");
print_listNode(result);
}