12-CRS-0106 REVISED 8 FEB 2013 KUG1C3 Dasar Algoritma dan Pemrograman.

Slides:



Advertisements
Similar presentations
CS 400/600 – Data Structures External Sorting.
Advertisements

CS252: Systems Programming Ninghui Li Program Interview Questions.
Understanding the Need for Sorting Records
Data Structures: A Pseudocode Approach with C
Chapter 10 Introduction to Arrays
Files  File organisation and usage A record is a group of logically related fields A file is a group of logically related records Files are used to store.
3/5/2009Computer systems1 Analyzing System Using Data Dictionaries Computer System: 1. Data Dictionary 2. Data Dictionary Categories 3. Creating Data Dictionary.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 24 Sorting.
CMPS1371 Introduction to Computing for Engineers SORTING.
Algorithm Design Techniques: Induction Chapter 5 (Except Sections 5.6 and 5.7)
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Programming Logic and Design Fourth Edition, Comprehensive
INTRODUCTION TO DATA STRUCTURE. Topics To Be Discussed………………………. Meaning of Data Structure Classification of Data Structure Data Structure Operations.
CS4432: Database Systems II
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
External Sorting Problem: Sorting data sets too large to fit into main memory. –Assume data are stored on disk drive. To sort, portions of the data must.
Traceback and local alignment Prof. William Stafford Noble Department of Genome Sciences Department of Computer Science and Engineering University of Washington.
HOW TO SOLVE IT? Algorithms. An Algorithm An algorithm is any well-defined (computational) procedure that takes some value, or set of values, as input.
1 Multiway trees & B trees & 2_4 trees Go&Ta Chap 10.
Triangulating a monotone polygon
Chapter 13 Sequential File Processing. Master Files Set of files used to store companies data in areas like payroll, inventory Usually processed by batch.
Computers Data Representation Chapter 3, SA. Data Representation and Processing Data and information processors must be able to: Recognize external data.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Hashing CS 202 – Fundamental Structures of Computer Science II Bilkent.
Database Management 9. course. Execution of queries.
Data and its manifestations. Storage and Retrieval techniques.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
12-CRS-0106 REVISED 8 FEB 2013 CSG2A3 ALGORITMA dan STRUKTUR DATA.
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 6: Sequential Data Files.
Multi-way Trees. M-way trees So far we have discussed binary trees only. In this lecture, we go over another type of tree called m- way trees or trees.
Indexed and Relative File Processing
External Storage Primary Storage : Main Memory (RAM). Secondary Storage: Peripheral Devices –Disk Drives –Tape Drives Secondary storage is CHEAP. Secondary.
1 Lab 2 and Merging Data (with SQL) HRP223 – 2009 October 19, 2009 Copyright © Leland Stanford Junior University. All rights reserved. Warning:
13-1 COBOL for the 21 st Century Nancy Stern Hofstra University Robert A. Stern Nassau Community College James P. Ley University of Wisconsin-Stout (Emeritus)
Sequential Files Chapter 13. Master Files Set of files used to store companies data in areas like payroll, inventory Set of files used to store companies.
Data Structure Introduction.
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures A bank is a place where they lend you an umbrella in fair weather and ask for it.
Pascal Programming Today Chapter 11 1 Chapter 11.
13-1 Sequential File Processing Chapter Chapter Contents Overview of Sequential File Processing Sequential File Updating - Creating a New Master.
13- 1 Chapter 13.  Overview of Sequential File Processing  Sequential File Updating - Creating a New Master File  Validity Checking in Update Procedures.
1 Algorithms Queues, Stacks and Records stored in Linked Lists or Arrays.
Review Sorting algorithms Selection Sort Insertion Sort Bubble Sort Merge Sort Quick Sort.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 25 Sorting.
Sorting. Sorting Sorting is important! Things that would be much more difficult without sorting: –finding a telephone number –looking up a word in the.
Flowcharts. Symbol Terminal Symbol: indicates the starting or stopping pointin the logic. Input/Output Symbol: Represents an input or output process in.
Chapter 11: Sequential File Merging, Matching, and Updating Programming Logic and Design, Third Edition Comprehensive.
11 Chapter 111 Sequential File Merging, Matching, and Updating Programming Logic and Design, Second Edition, Comprehensive 11.
Selection Sort Given an array[0-N], place the smallest item in the array in position 0, the second smallest in position 1, and so forth. We do thisby comparing.
Chapter 5 Record Storage and Primary File Organizations
Files A collection of related data treated as a unit. Two types Text
CHAPTER 51 LINKED LISTS. Introduction link list is a linear array collection of data elements called nodes, where the linear order is given by means of.
BITS Pilani Pilani Campus Data Structure and Algorithms Design Dr. Maheswari Karthikeyan Lecture3.
LINKED LISTS.
5.13 Recursion Recursive functions Functions that call themselves
Learning Objectives Today we will Learn:
The Data Types and Data Structures
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
File Input/Output.
Binary Files.
Databases Lesson 2.
Programming Logic and Design Fourth Edition, Comprehensive
Computer Science II First With files.
Computer Science II First With files.
Designing and Writing Control-Break Programs
Lecture 2- Query Processing (continued)
Lab 2 and Merging Data (with SQL)
Please use speaker notes for additional information!
Computer Science II First With files.
Presentation transcript:

12-CRS-0106 REVISED 8 FEB 2013 KUG1C3 Dasar Algoritma dan Pemrograman

12-CRS-0106 REVISED 8 FEB 2013 Let’s say we have two (2) already sorted arrays Merge those 2 into 1 sorted array without using any sorting algorithm 2 Merging Arrays ABC

12-CRS-0106 REVISED 8 FEB 2013 Check top element of both array A and B Insert the least element into array C Loop until one of array is empty Copy the rest element of the other array into array C 3 Merging Arrays - Logic

12-CRS-0106 REVISED 8 FEB 2013 Dictionary tab_a, tab_b : array[1..10] of integer // assume tab_a and tab_b already defined tab_c: array[1..20] of integer n_a, n_b: integer // number of element tab_a and tab_b i, j, k: integer 4 Merging Arrays Algorithm i  1; j  1; k  1 while ( i ≠ n_a ) and ( j ≠ n_b ) do if ( tab_a[ i ] < tab_b[ j ] ) then tab_c[ k ]  tab_a[ i ] k  k + 1 i  i + 1 else tab_c[ k ]  tab_b[ j ] k  k + 1 j  j + 1 while ( i ≠ n_a ) do tab_c[ k ]  tab_a[ i ] k  k + 1 i  i + 1 while ( j ≠ n_b ) do tab_c[ k ]  tab_b[ j ] k  k + 1 j  j + 1

12-CRS-0106 REVISED 8 FEB 2013 Let’s say we have an array of integer Split the array into two arrays by even and odd number 5 Splitting Array by some category even oddTab_int

12-CRS-0106 REVISED 8 FEB 2013 Dictionary tab_a: array[1..10] of integer // assume tab_a already defined tab_b, tab_c: array[1..20] of integer n_a: integer // number of element tab_a i, j, k: integer 6 Splitting Array Algorithm i  1; j  1; k  1 i traversal [1.. n_a ] if ( tab_a[ i ] mod 2 = 0) then tab_b[ j ] = tab_a[ i ] j  j + 1 else tab_c[ k ] = tab_a[ i ] k  k + 1

12-CRS-0106 REVISED 8 FEB 2013 Question?

12-CRS-0106 REVISED 8 FEB 2013 KUG1C3 Dasar Algoritma dan Pemrograman

12-CRS-0106 REVISED 8 FEB Sequential File Stored in secondary memory storage Sequential File is a set of records that are stored into a secondary storage media computer and accessed sequentially, in the same direction

12-CRS-0106 REVISED 8 FEB Logic Structure of Sequential File  Access sequence one-by- one MARKRec-1 Rec-2Rec-3Rec-N Vertical logic structure Rec-1 Rec-2 Rec-3  Rec-N MARK Access direction Horizontal logic structure

12-CRS-0106 REVISED 8 FEB 2013 Dictionary type record_type : // type defined for each record doc_name : SEQFILE of (*) : record_type (1) 11 Defining Sequential File

12-CRS-0106 REVISED 8 FEB 2013 Dictionary type rec_char : character c : rec_char doc_char : SEQFILE of (*)cc : rec_char (1) Algorithm 12 Example : character record

12-CRS-0106 REVISED 8 FEB 2013 Dictionary type rec_mhs: < nim: integer name: string score: integer [1..100] > m1 : rec_mhs doc_mhs : SEQFILE of (*) m : rec_mhs (1) Algorithm 13 Example : student record

12-CRS-0106 REVISED 8 FEB 2013 Procedure ASSIGN ( input doc_name, filename) –Open physical file and create a connection to read and write via doc_name –On class we don’t use this function –Example : ASSIGN ( doc_mhs, ‘mhs.txt’ ) 14 Primitive Function on Sequential File

12-CRS-0106 REVISED 8 FEB 2013 Procedure OPEN ( input doc_name, ) –Open the connection to the document so the file is ready to be accessed (read/write) –The first record on file (if any) will be read into record_var –Example : OPEN ( doc_mhs, m ) OPEN ( doc_char, cc ) 15 Primitive Function on Sequential File

12-CRS-0106 REVISED 8 FEB 2013 Procedure READ ( input doc_name, ) –Read the next record on file –IS : record_var is not mark (EOF) –FS : record_var contains the next record on file –Example :READ ( doc_mhs, m ) READ ( doc_char, cc ) 16 Primitive Function on Sequential File

12-CRS-0106 REVISED 8 FEB 2013 Procedure REWRITE ( input/output doc_name) –Clear (empty) the file –Example : REWRITE ( doc_mhs ) REWRITE ( doc_char ) 17 Primitive Function on Sequential File

12-CRS-0106 REVISED 8 FEB 2013 Procedure WRITE ( input doc_name, ) –Write a record into the file –The pointer move to next –IS : record_var is not null –FS : record_var saved into file –Example :WRITE ( doc_mhs, m ) WRITE ( doc_char, cc ) 18 Primitive Function on Sequential File

12-CRS-0106 REVISED 8 FEB 2013 To write a mark element : –Example : WRITE ( doc_mhs, ) WRITE ( doc_char, ) –Once the mark is written, the document can no longer be accessed –Define yourself the mark element 19 Primitive Function on Sequential File

12-CRS-0106 REVISED 8 FEB 2013 Procedure CLOSE ( input doc_name) –Close the connection to the document –The document can no longer be accessed –Example : CLOSE ( doc_mhs ) CLOSE ( doc_char ) 20 Primitive Function on Sequential File

12-CRS-0106 REVISED 8 FEB 2013 Dictionary type mahasiswa: doc_mhs : SEQFILE of (*) m : mahasiswa (1) again: char Algorithm OPEN ( doc_mhs, m ) {read first-elmt} while ( m.nim ≠ ) do READ ( doc_mhs, m ) again  ‘y’; 21 Example : input and save student data While ( again = ‘y’ ) do Input( m.nim ) Input( m.score ) WRITE ( doc_mhs, m ) Input( again ) WRITE ( doc_mhs, ) CLOSE ( doc_mhs )

12-CRS-0106 REVISED 8 FEB 2013 Dictionary type mahasiswa: doc_mhs : SEQFILE of (*) m : mahasiswa (1) total_score : integer total_mhs: integer Algorithm OPEN ( doc_mhs, m ) {read first-elmt} If ( m.nim = ) then output ( ‘empty document’ ) 22 Example : read student data, output the score average Else total_score  0 JumMhs  0 Repeat total_score := total_score + m.nilai total_mhs := total_mhs + 1 READ ( doc_mhs, m ) {read next-elmt} Until ( m.nim = ) Output ( total_score / total_mhs ) CLOSE ( doc_mhs )

12-CRS-0106 REVISED 8 FEB 2013 Logic : –Read all data –Write all data back to the file except the data that want to be removed 23 Remove a data from file

12-CRS-0106 REVISED 8 FEB 2013 Dictionary type mahasiswa: doc_mhs : SEQFILE of (*) m : mahasiswa (1) new_doc : SEQFILE of (*) m2 : mahasiswa (1) nim_remove: integer Algorithm OPEN ( doc_mhs, m ) OPEN ( new_doc, m2 ) 24 Example : remove a data If ( m.nim = ) then output ( ‘empty document’ ) Else input( nim_remove ) Repeat if( m.nim ≠ nim_remove ) then WRITE ( new_doc, m ) READ ( doc_mhs, m ) {read next-elmt} Until ( m.nim = ) CLOSE ( doc_mhs )

12-CRS-0106 REVISED 8 FEB 2013 Example : –Insert into the n-th row Logic : –Read all data and write again to a new file until the (n-1) th data –Write the data that wants to be inserted to the new file –Write the rest of data 25 Insert a data in the middle of the file

12-CRS-0106 REVISED 8 FEB Merging Files Combining 2 files into one file The simplest way is by adding the second file into end of the first file

12-CRS-0106 REVISED 8 FEB Merging Files But if the files are already sorted and we want to combine 2 files into one sorted file, the previous technique can not be used Create algorithm to combine two sorted files Remember merging array

12-CRS-0106 REVISED 8 FEB 2013 Dictionary type mahasiswa : < nim : integer, score : integer[1..100] > doc_mhs1 : SEQFILE of (*) m1 : mahasiswa (1) doc_mhs2 : SEQFILE of (*) m2 : mahasiswa (1) doc_merge : SEQFILE of (*) m3 : mahasiswa (1) Algorithm OPEN ( doc_mhs1, m1 ) {open first doc} OPEN ( doc_mhs2, m2 ) {open second doc} REWRITE ( doc_merge ) {empty the merge doc} 28 Merging Files while ( m1.nim ≠ ) and ( m2.nim ≠ ) do if (m1.nim < m2.nim ) then WRITE( doc_merge, m1 ) READ( doc_mhs1, m1 ) else WRITE(doc_merge, m2 ) READ( doc_mhs2, m2 ) while ( m1.nim ≠ ) do WRITE( doc_merge, m1 ) READ( doc_mhs1, m1 ) while ( m2.nim ≠ ) do WRITE( doc_merge, m2 ) READ( doc_mhs2, m2 ) WRITE( doc_merge, ) CLOSE( doc_mhs1 ) CLOSE( doc_mhs2 ) CLOSE( doc_merge )

12-CRS-0106 REVISED 8 FEB 2013 Change value of a record inside a master file using data from transaction file Change data based on an ID Example : –Update the balance of account data in a bank 29 Updating using Transaction File

12-CRS-0106 REVISED 8 FEB Updating using Transaction File type account_redord: Master File : Transaction File : New Master File : Updating balance record of the accounts in a bank

12-CRS-0106 REVISED 8 FEB 2013 Logic : –Assuming the transaction file and the master file is already sorted –Read the transaction file –Look into the master file –If the ID are match, update the balance 31 Updating using Transaction File

12-CRS-0106 REVISED 8 FEB 2013 Dictionary type account: master_doc : SEQFILE of (*) acc_m : mahasiswa (1) transcaction : SEQFILE of (*) acc_t : mahasiswa (1) new_master : SEQFILE of (*) acc_m2 : mahasiswa (1) newBalance : integer Algorithm OPEN ( master_doc, acc_m ) OPEN ( transcaction, acc_t ) REWRITE ( new_master ) 32 Updating Files while ( acc_m.id ≠ 9999 ) do {read old master file} {find the id in the transaction file} while ( acc_t.id < acc_m.id ) and ( acc_t.id ≠ 9999 ) do READ( transcaction, acc_t ) {if the id found, update the balance} if ( acc_t.id = acc_m.id ) then newBalance  acc_m.balance repeat newBalance  newBalance + acc_t.balance READ( transcaction, acc_t ) until (acc_t.id ≠ acc_m.id ) or ( acc_t.id ≠ 9999 ) WRITE( new_master, ) else {if id not found, copy old data into new master file } WRITE( new_master, acc_m ) READ( master_doc, acc_m ) {read next old data} WRITE( new_master, ) CLOSE( master_doc ) CLOSE( transcaction ) CLOSE( new_master )

12-CRS-0106 REVISED 8 FEB 2013 One file storing multiple type of data The type (group) is identified by the ID of each data or a mark (separator) between each type 33 File Consolidation

12-CRS-0106 REVISED 8 FEB 2013 Each type (group) of data has its own ID The type is identified by the key/ID changing 34 Consolidation model 1

12-CRS-0106 REVISED 8 FEB Example 1 : score average of student record type student : Student file : Student 001 : 75 Student 002 : 77.5 Student 003 : 85.6 Student 004 : 87

12-CRS-0106 REVISED 8 FEB 2013 Dictionary type mahasiswa: < id : integer, subject : string, score: integer > doc_mhs : SEQFILE of (*) m : mahasiswa (1) current_id: integer total_score, n: integer Algorithm OPEN ( doc_mhs, m ) {read first-elmt} If ( m.nim = 9999 ) then output ( ‘empty document’ ) 36 Example 1 : score average of student record else repeat current_id  m.nim total_score  0 n  0 repeat {read the scores of the student} total_score  total_score + m.score n  n + 1 READ ( doc_mhs, m ) until ( current_id ≠ m.nim ) Output ( total_score / n ) Until ( m.nim = 9999 ) CLOSE ( doc_mhs )

12-CRS-0106 REVISED 8 FEB 2013 The type is identified by a separator –In the example the separator is a ‘white card’ 37 Consolidation model 2

12-CRS-0106 REVISED 8 FEB Example 2 : score average of student record Student file : Student 001 : 75 Student 002 : 77.5 Student 003 : 85.6 Student 004 : 87

12-CRS-0106 REVISED 8 FEB 2013 Dictionary type mahasiswa: < id : integer, subject : string, score: integer > doc_mhs : SEQFILE of (*) m : mahasiswa (1) total_score, n: integer Algorithm OPEN ( doc_mhs, m ) {read first-elmt} If ( m.nim = 9999 ) then output ( ‘empty document’ ) 39 Example 2 : score average of student record else repeat {skip separator} while (m.nim ≠ 9999 ) and (m.nim = 0) do READ ( doc_mhs, m ) total_score  0 n  0 while (m.nim ≠ 9999 ) and (m.nim ≠ 0) do total_score  total_score + m.score n  n + 1 READ ( doc_mhs, m ) if ( n ≠ 0 ) then Output ( total_score / n ) Until ( m.nim = 9999 ) CLOSE ( doc_mhs )

12-CRS-0106 REVISED 8 FEB 2013 Question?

12-CRS-0106 REVISED 8 FEB 2013 THANK YOU 41