• Question :

    • Bubble sort using singly-linked list
    • 群暉面試題
  • Idea : 
    • 在linked list 交換node與node時, 我們會想用換*next的方式。但是這裡是singly-linked list.
      還會需要previously node。其實這裡單純直接換int val還比較簡單。不過算蠻偷吃步的
  • Code : [根本就是array版]
  • typedef struct Node{
    int val;
    struct Node *next;
    }Node, *NodePtr;
    void bubbleSort(NodePtr head) {
    int i,j,tmp;
    bool flag;
    for(i = 0; i < len-1 ;i++) {
    NodePtr curr = head;
    NodePtr next = head->next;
    flag = false;
    for(j=0; j < len-i-1 ;j++) {
    if(curr->val > next->val ) {
    tmp = curr->val;
    curr->val = next->val;
    next->val = tmp;
    flag = true;
    }
    }
    if(flag == false) break; // 沒有結點需要swap
    }
    }
  •   [修改*next 版]
void bubbleSort(NodePtr head) {
bool flag = true;
while(flag) {
flag = false;
NodePtr curr = head;
NodePtr prev = NULL;
while(curr->next){
if(curr->val > curr->next->val) {
flag = true;
curr = swap(curr,curr->next);
if(prev == NULL) //while swaping the head node
head = curr;
else
prev->next = curr;
}
prev = curr;
curr = curr->next;
}
}
}

    [使用dummyHead版] - 可以減少一個if, 來判斷swap頭結點的情況。

void bubbleSort(NodePtr head) {
bool flag = true;
NodePtr dummyHead = (NodePtr)malloc(sizeof(Node));
dummyHead->next = head;
while(flag) {
flag = false;
NodePtr curr = dummyHead->next;
NodePtr prev = dummyHead;
while(curr->next){
if(curr->val > curr->next->val) {
flag = true;
prev->next = curr = swap(curr,curr->next);
}
prev = curr;
curr = curr->next;
}
printf("flag:%d ",flag);
print_all(dummyHead->next);
}
}

  

  • 完整測資 : https://github.com/bittorrent3389/leetCode/blob/master/BubbleSort.c

[Interview] Bubble sort using singly-linked list的更多相关文章

  1. [LintCode] Delete Node in the Middle of Singly Linked List 在单链表的中间删除节点

    Implement an algorithm to delete a node in the middle of a singly linked list, given only access to ...

  2. Java中的经典算法之冒泡排序(Bubble Sort)

    Java中的经典算法之冒泡排序(Bubble Sort) 神话丿小王子的博客主页 原理:比较两个相邻的元素,将值大的元素交换至右端. 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面.即在第一 ...

  3. LeetCode 206 Reverse a singly linked list.

    Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. ...

  4. Bubble Sort (5775)

    Bubble Sort Problem Description   P is a permutation of the integers from 1 to N(index starting from ...

  5. Singly Linked List

    Singly Linked List Singly linked list storage structure:typedef struct Node{ ElemType data; struct N ...

  6. Bubble Sort [ASM-MIPS]

    # Program: Bubble sort # Language: MIPS Assembly (32-bit) # Arguments: 5 unordered numbers stored in ...

  7. HDU 5775 Bubble Sort(冒泡排序)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  8. Reverse a singly linked list

    Reverse a singly linked list. /** * Definition for singly-linked list. * struct ListNode { * int val ...

  9. 2016 Multi-University Training Contest 4 Bubble Sort(树状数组模板)

    Bubble Sort 题意: 给你一个1~n的排列,问冒泡排序过程中,数字i(1<=i<=n)所到达的最左位置与最右位置的差值的绝对值是多少 题解: 数字i多能到达的最左位置为min(s ...

随机推荐

  1. Robot Framework 源码阅读 day1 __main__.py

    robot文件夹下的__main__.py函数 是使用module运行时的入口函数: import sys # Allows running as a script. __name__ check n ...

  2. 【记录】form-data与x-www-form-urlencoded的区别

    1)application/x-www-form-urlencoded 这应该是最常见的 POST 提交数据的方式了.浏览器的原生 <form> 表单,如果不设置 enctype 属性,那 ...

  3. Elasticsearch Java Low Level REST Client(嗅探器)

    https://segmentfault.com/a/1190000016828977?utm_source=tag-newest#articleHeader0 嗅探器 允许从正在运行的Elastic ...

  4. AD转换为KiCAD的方法

    一.Altium文件转KiCad文件 本文主要介绍: 1.AD文件(SCH和PCB)转换为KiCAD的方法 2.AD封装库转换为KiCAD库的方法 下面让我们进入正题 1.1 PCB的第一种转换方式 ...

  5. 12 | 为什么我的MySQL会“抖”一下? 学习记录

    <MySQL实战45讲>12 | 为什么我的MySQL会“抖”一下? 学习记录 http://naotu.baidu.com/file/15aa54cab2fa882c6a2a1dd52e ...

  6. Jquery异步上传文件

    我想通过jQuery异步上传文件,这是我的HTML: 1 2 3 <span>File</span> <input type="file" id=&q ...

  7. 容器(collection)初步

    容器(集合)的分类: 泛型(generic):本质是数据类型的参数化(提前告诉编译器,在调用泛型时必须传入实际类型) 例:E即为在主函数中定义的传入的实际类型 class MyCollection&l ...

  8. Flask 之装饰器有关

    - 先记住一句话:自定义python装饰器时一定要记住使用@functools.wraps(func)修饰wrapper - 在Flask中使用装饰器遇到AssertionError: View fu ...

  9. 【leetcode】1026. Maximum Difference Between Node and Ancestor

    题目如下: Given the root of a binary tree, find the maximum value V for which there exists different nod ...

  10. SQL Server性能调优--索引

    序言 索引的概念 索引是什么 数据库中的索引类似于一本书的目录,在一本书中使用目录可以快速找到你想要的信息,而不需要读完全书.在数据库中,数据库程序使用索引可以快速查询到表中的数据,而不必扫描整个表. ...