프로그래밍2 및 실습 Sort Code 2016. 10. 25 전명중.

Slides:



Advertisements
Similar presentations
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Advertisements

PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Linked List Variations
Doubly linked list concept Node structure Insertion sort Insertion sort program with a doubly linked list.
Doubly-linked list library.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Add two strings representing numbers Carry = 0 S1 = S2 = S3 =
Chapter 17 Linked List Saurav Karmakar Spring 2007.
 2007 Pearson Education, Inc. All rights reserved. Structs as Function Arguments and Results  Arrays – Pass by referance  Struts – the same way as the.
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
CCSB364 Data Structures & Algorithms Pointer & Linked List.
Topic 2 Pointers CSE1303 Part A, Summer Semester,2002 Data Structures and Algorithms.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A2 – Pointers (Revision)
Pointers Chapters 6+9 in ABC. abp 12 int a = 1, b = 2, *p; & - reference operator (address) * - dereference operator (value) p = &a; // *p is now 1 abp.
Insertion Sorting Lecture 21. Insertion Sort Start from element 2 of list location 1 –In first iteration: Compare element 1 with all of its elements to.
CSSE 332 Explicit Memory Allocation, Parameter passing, and GDB.
CS 470/570 Lecture 7 Dot Product Examples Odd-even transposition sort More OpenMP Directives.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Complex Structures Nested Structures Self referential structures A structure may have Data variables Internal structures/unions Pointer links Function.
Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.
CPT: Arrays of Pointers/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to illustrate the use of arrays.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Code Update 01/16/2013. Agenda OOP API Update (Semi-)Automated Composition.
Generic Functions1 Generic Functions: A generic function is one that can work on any underlying C data type. Generic functions allow us to reuse programs.
Introduction to C Programming CE Lecture 24 Insertion and Deletion with Binary Search Trees.
CS1010E Programming Methodology Tutorial 9 Pointers in Arrays & Structures C14,A15,D11,C08,C11,A02.
1 Objectives of these slides: to describe linked lists in C 6 – Lists.
Bits and Bytes September 1, F’05 class02.ppt “The Class That Gives CMU Its Zip!”
Pre/Post Condition Discussion 03/13/2013. Quicksort int main(int argc, char* argv[]) { int numbers[SIZE] = {2,5,3…} struct Sorter s; // initialize generic.
Pointer Lecture 2 Course Name: High Level Programming Language Year : 2010.
Programming Linked Lists. Collections Store collection of data  Online store - Items  University – Students  Library – books Until now we used arrays.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Dale Roberts, Lecturer Data Structure.
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Array Sort. Sort Pass 1 Sort Pass 2 Sort Pass 3.
CPT: Lists/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to describe linked lists in C 15. Lists.
C Tutorial - Pointers CS 537 – Introduction to Operating Systems.
Pointers (Revision). 2 Overview Revision of Pointers Pointers and structs Basic Pointer Arithmetic Pointers and Arrays.
CPSC 221 Call by value, pointer and reference in C++ Page 1 Hassan Khosravi January – April 2015 CPSC 221 Basic Algorithms and Data Structures Call by.
Lecture No.04 Data Structures Dr. Sohail Aslam
Linked List.
Understand argc and argv
Problems with dynamic arrays
CSC172 Data Structures Linked Lists (C version)
Introduction to Programming
LinkedList Class.
קורס תכנות שיעור עשירי: מיונים, חיפושים, קצת ניתוח זמני ריצה, קצת תיקון טעויות ועוד על רשימות.
קורס תכנות שיעור 14: הסוף.
Chapter 16-2 Linked Structures
More Examples of argc and argv
Programming -2 برمجة -2 المحاضرة-5 Lecture-5.
Yung-Hsiang Lu Purdue University
קורס תכנות שיעור 13: רשימות מקושרות.
הרצאה 08 פרמטרים ל- main קרן כליף.
ليست هاي پيوندي.
Linked Lists (continued)
Programming Linked Lists.
Introduction to Programming
Computer Programming תירגול 13 חזרה למבחן.
תכנות בשפת C תרגול 12 עצים בינאריים
Example. Sort {5, 1, 12, -5, 16, 2, 12, 14} using selection sort. Complexity analysis.
The Big 5 and Lists.
By C. Shing ITEC Dept Radford University
CS148 Introduction to Programming II
(PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz
Linked Lists.
Presentation transcript:

프로그래밍2 및 실습 Sort Code 2016. 10. 25 전명중

int main(int argc, const char * argv[]) { listNode *L; L = addFrontNode(L, "JEON", 95); L = addFrontNode(L, "PARK", 90); L = addFrontNode(L, "KANG", 42); L = addFrontNode(L, "LEE", 80); L = addFrontNode(L, "CHOI", 50); showAllInList(L); printf("[selection]----------------\n"); L = selectionSortByGrade(L); printf("[bubble]----------------\n"); L = bubbleSortByGrade(L); return 0; } typedef struct ListNode{ char *name; int grade; struct ListNode *link; } listNode; listNode* bubbleSortByGrade(listNode *); listNode* selectionSortByGrade(listNode *); int getListSize(listNode *); void showAllInList(listNode *); listNode* addFrontNode(listNode *, char *, int); listNode* createNode(char *, int);

Create Node listNode* createNode(char *name, int grade){ listNode* newNode = (listNode *)malloc(sizeof(listNode)); newNode->name = name; newNode->grade = grade; newNode->link = NULL; return newNode; }

Add Node to List listNode* addFrontNode(listNode *L, char *name, int grade){ // make a new node listNode *newN = createNode(name, grade); // if first node if( L == NULL ){ L = newN; return L; } // if existing list, add to the first position. newN->link = L;

Add Node to List & get Size void showAllInList(listNode *L){ listNode *temp = L; int i = 0; while (temp != NULL) { printf("[%d] %s : %d\n", i++, temp->name, temp->grade); temp = temp->link; } int getListSize(listNode *L){ listNode *temp = L; int i = 0; while (temp != NULL) { i++; temp = temp->link; } return i;

Selection Sort listNode* selectionSortByGrade(listNode *L){ listNode *curr = L; while ( curr != NULL ) { // last node if ( curr->link == NULL ){ break; } listNode *temp = curr->link; listNode *min = temp; while ( temp != NULL ) { if (min->grade > temp->grade){ min = temp; temp = temp->link; if ( curr->grade > min->grade ){ int tmpGrade = min->grade; min->grade = curr->grade; curr->grade = tmpGrade; curr = curr->link; return L; Selection Sort

Bubble Sort listNode* bubbleSortByGrade(listNode *L){ int len = getListSize(L); // if length of list is less than 1 if (len <= 1) return L; listNode *p1 = L; listNode *p2 = L->link; for ( int i = len - 1; i >= 0; i-- ){ // init p1 = L; p2 = L->link; for ( int j = 0; j < i; j++ ){ if ( p1->grade > p2->grade ){ // swap int tmp = p1->grade; p1->grade = p2->grade; p2->grade = tmp; }// if // move next position p1 = p1->link; p2 = p2->link; } Bubble Sort