James Tam Sorting (Bubble) In this section of notes you will learn one technique for ordering a list.

Slides:



Advertisements
Similar presentations
James Tam Linked Lists in Pascal Linked Lists In this section of notes you will learn how to create and manage a dynamic list.
Advertisements

J. Michael Moore Text Files CSCE 110 From James Tam’s material.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
James Tam Records You will learn in this section of notes how to create a new, composite type, that can be composed of different types of elements.
James Tam Multi-Dimensional Arrays In Pascal In this section of notes you will learn about how and when to use multi- dimensional arrays.
James Tam Repetition In Pascal In this section of notes you will learn how to have a section of code repeated without duplicating the code.
James TamThe Bubble Sort in Pascal Sorting (Bubble) In this section of notes you will learn one technique for ordering a list.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
James Tam Introduction To Files In Python In this section of notes you will learn how to read from and write to files in your programs.
James Tam Breaking Problems Down This section of notes shows you how to break down a large programming problem into smaller modules that are easier to.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
James Tam Sorting (Bubble) In this section of notes you will learn one technique for ordering a list.
James Tam Sorting (Bubble) In this section of notes you will learn one technique for ordering a list.
James Tam Introduction To Files In Pascal In this section of notes you will learn how to read from and write to files in Pascal.
James Tam Arrays In this section of notes you will be introduced to a composite type where all elements must be of the same type (homogeneous): arrays.
James Tam Repetition In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
James Tam Introduction To Files In Pascal In this section of notes you will learn how to read from and write to files in your Pascal programs.
James Tam Records You will learn in this section of notes how to create a new, composite type, that can be composed of different types of elements.
James Tam Multi-Dimensional Arrays In Pascal In this section of notes you will learn about how and when to use multi- dimensional arrays.
J. Michael Moore From James Tam’s material Multi-Dimensional Arrays CSCE 110.
James Tam Linked Lists In this section of notes you will learn how to create and manage a dynamic list.
James Tam Sorting (Bubble) In this section of notes you will learn one technique for ordering a list.
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Records You will learn in this section of notes how to create a new, composite type, that can be composed of different types of elements.
J. Michael Moore Arrays CSCE 110 From James Tam’s material.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
James Tam program investors (input, output, investorList, updatedInvestorList); const MAXCLIENTS = 10; NAMELENGTH = 30; LENGTH = 30; type Client =
James Tam Records You will learn in this section of notes how to create a new, composite type, that can be composed of different types of elements.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
James Tam Linked Lists In this section of notes you will learn how to create and manage a dynamic list.
James Tam Linked Lists In this section of notes you will learn how to create and manage a dynamic list.
James Tam Breaking Problems Down This section of notes shows you how to break down a large programming problem into smaller modules that are easier to.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
Program Design and Development
J. Michael Moore From James Tam’s material Multi-Dimensional Arrays CSCE 110.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate your code.
James Tam Arrays In this section of notes you will be introduced to a homogeneous composite type, one- dimensional arrays.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
J. Michael Moore From James Tam's material Records CSCE 110.
J. Michael Moore Other Control Structures CSCE 110 Influenced by material developed by James Tam & Jennifer Welch.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
James Tam Breaking Problems Down This section of notes shows you how to break down a large programming problem into smaller modules that are easier to.
James Tam Records You will learn in this section of notes how to create a new, composite type, that can be composed of different types of elements.
James Tam Linked Lists In this section of notes you will learn how to create and manage a dynamic list.
Link Lists In this section of notes you will learn how to create and manage a dynamic list. Link Lists in Pascal.
James Tam Introduction To Files In Pascal You will learn how to read from and write to text files in Pascal.
J. Michael Moore Arrays CSCE 110. J. Michael Moore Typical (although simplified) Problem Write a program that will track student grades in a class. The.
James Tam Records You will learn in this section of notes what is a record and how to use them in Pascal.
James Tam Breaking Problems Down This section of notes shows you how to break down a large problem into smaller modules that are easier to implement and.
James Tam Introduction To Files In Pascal In this section of notes you will learn how to read from and write to files in Pascal.
James Tam Introduction To Files In Pascal In this section of notes you will learn how to read from and write to text files in Pascal.
James Tam Introduction To Files In Python In this section of notes you will learn how to read from and write to files in your programs.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
# 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.
Chapter 6: Arrays: Lists and Tables
Introduction to Pascal The Basics of Program writing.
Sha Tin Methodist College F.4 Computer Studies Pascal Programming.
Programming, an introduction to Pascal
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Using Text Files in Excel File I/O Methods. Working With Text Files A file can be accessed in any of three ways: –Sequential access: By far the most common.
Pascal Programming Today Chapter 11 1 Chapter 11.
CS241 PASCAL I - Control Structures1 PASCAL Control Structures Modified Slides of Philip Fees.
PASCAL PROGRAMMING.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 11.
FOR LOOP STRUCTURE For := to do eg. for I := 1 to 100 do begin writeln(‘This is a loop’); end;
Sorting (Bubble) In this section of notes you will learn one technique for ordering a list.
Computer Science 1 For..do loop Dry Run Take notes on the For loop
Sorting and Complexity
Presentation transcript:

James Tam Sorting (Bubble) In this section of notes you will learn one technique for ordering a list

James Tam Algorithm: Original List EADBA

James Tam Algorithm: First Pass Through The List 1 st swap AEDBA 2 nd swap ADEBA 3 rd swapADBEA 4 th swap EADBAStart ADBAE

James Tam Algorithm: Second Pass Through The List 1 st swap ABDAE 2 nd swap ABADE ADBAEStart

James Tam Algorithm: Third Pass Through The List 1 st swap AABDE ABADEStart

James Tam Algorithm: Fourth Pass Through The List AABDEStart

James Tam A Sorting Example A complete version of this example program can be found under /home/231/examples/sorting/investors.p program investors (input, output); const MAX_CLIENTS = 10; NAME_LENGTH = 30; _LENGTH = 30; FILE_NAME_LENGTH = 256; type Client = record firstName : string [NAME_LENGTH]; lastName : string [NAME_LENGTH]; income : real; string [ _LENGTH]; end; (* Declaration of record client *) ClientList = array [1..MAX_CLIENTS] of Client;

James Tam A Sorting Example (2) procedure readClientInformation (var aClientList : ClientList ; var clientCount : integer); var investorData : text; inputFileName : string[FILE_NAME_LENGTH]; begin; writeln; write('Enter the name of the input file: '); readln(inputFileName); reset(investorData, inputFileName); writeln('Opening file ', inputFileName, ' for reading'); clientCount := 0; if EOF (investorData) then begin writeln('File ', inputFileName, ' is empty: nothing to read.'); end

James Tam A Sorting Example (3) else begin while NOT EOF (investorData) AND (clientCount < MAX_CLIENTS) do begin clientCount := clientCount + 1; with aClientList[clientCount] do begin readln(investorData, firstName); readln(investorData, lastName); readln(investorData, income); readln(investorData, ); readln(investorData); (* Eat the newline between records *) end; end; (* while *) end; (* else *) close(investorData); end; (* End of procedure readClientInformation *)

James Tam A Sorting Example (4) procedure displayInstructions; begin writeln; writeln('This program allows you track a list of clients each of which is'); writeln('is an investor. The initial investor information will be read'); writeln('from the file called "investorList". From there you can display'); writeln('the list of clients onscreen, add a client, modify the'); writeln('information for a client, erase clients from the list, recorder'); writeln('the list of clients or search for a client. When you are done'); writeln('quit the program and all of your changes will be written out to'); writeln('an output file.'); end;

James Tam A Sorting Example (5) procedure displayClientList (aClientList : ClientList; clientCount : integer ); var i : integer; begin writeln; writeln('DISPLAYING CLIENT LIST'); writeln(' '); if (clientCount = 0) then writeln('No clients in the list: nothing to display');

James Tam A Sorting Example (6) for i := 1 to clientCount do begin with aClientList[i] do begin writeln('First name: ':12, firstName); writeln('Last name: ':12, lastName); writeln('Income :$':12, income:0:2); writeln(' ':12, ); end; (*with-do *) writeln; end; (* for *) end; (* End of procedure displayClientList *)

James Tam A Sorting Example (7) procedure addClient (var aClientList : ClientList; var clientCount : integer); var newClient : Client; begin writeln; writeln('Adding new client to list of clients');

James Tam A Sorting Example (8) with newClient do begin write('Enter first name of client (max 30 characters): '); readln(firstName); write('Enter last name of client (max 30 characters): '); readln(lastName); write('Enter annual gross income of client (max 8 digits with no commas) $'); readln(income); write('Enter of client (max 30 characters):'); readln( ); writeln; end; (* with-do *) clientCount := clientCount + 1; aClientList[clientCount] := newClient; writeln('Added new client ', newClient.lastName); end; (* End of procedure addClient *)

James Tam A Sorting Example (9) procedure swap (var first : Client; var second : Client); var temp : Client; begin temp := first; first:= second; second := temp; end; (* End of procedure swap *)

James Tam A Sorting Example (10) procedure reorder (var aClientList : ClientList; clientCount : integer); var i : integer; isSorted : boolean; begin

James Tam A Sorting Example (11) repeat begin (* Assume list is sorted until proven otherwise. *) isSorted := True; for i := 1 to (clientCount-1) do begin if (aClientList[i].lastName > aClientList[i+1].lastName) then begin swap(aClientList[i], aClientList[i+1]); isSorted := False; end; (* if-then *) end; (* for *) end; (* repeat-until *) until (isSorted = True); end; (* End of procedure reorder *)

James Tam A Sorting Example (12) procedure saveClientInformation (aClientList : ClientList; clientCount : integer); var i : integer; upDatedInvestorData : text; outputFileName : string[FILE_NAME_LENGTH]; begin writeln; write('Enter the name of the file to save client information to: '); readln(outputFileName); rewrite(updatedInvestorData, outputFileName); writeln('Saving updated investor information to file ', outputFileName);

James Tam A Sorting Example (13) for i := 1 to clientCount do begin with aClientList[i] do begin writeln(updatedInvestorData, firstName); writeln(updatedInvestorData, lastName); writeln(updatedInvestorData, income:0:2); writeln(updatedInvestorData, ); writeln(updatedInvestorData); end; writeln(updatedInvestorData); close(updatedInvestorData); end; (* End of procedure saveClientInformation *)

James Tam A Sorting Example (14) procedure displayMenu; begin writeln; writeln('Options'); writeln('(d)isplay list of clients'); writeln('(a)dd a new client'); writeln('(e)rase a client''s record from the list'); writeln('(m)odify an existing client'); writeln('(r)eorder the client list alphabetically by last name'); writeln('(s)earch for a client'); writeln('(q)uit the program.'); write('Enter your selection: '); end; (* End of procedure displayMenu *)

James Tam A Sorting Example (15) procedure processMenuSelection (var aClientList : ClientList; var clientCount : integer; menuSelection : char); begin case (menuSelection) of 'D', 'd' : begin displayClientList(aClientList, clientCount); end; 'A', 'a' : begin addClient(aClientList, clientCount); end;

James Tam A Sorting Example (16) 'E', 'e' : begin writeln; writeln('Erase client record: You need to write the code to do'); writeln('this in your own program.'); end; 'M', 'm' : begin writeln; writeln('Modify client: You need to write the code to do this'); writeln('in your own program.'); end; 'R','r' : begin writeln; reorder (aClientList, clientCount); end;

James Tam A Sorting Example (17) 'S', 's' : begin writeln; writeln('Search for a client: You need to write the code to do'); writeln('this in your own program.'); end; 'Q', 'q' : begin writeln; writeln('Thank you for using the investor 2000 (TM) program.'); writeln('Come again!'); writeln; end;

James Tam A Sorting Example (18) else begin writeln; writeln('Please enter one of the following options: ''d'',''a''', '''e'',''m'',''r'',''s'' or ''q'''); writeln; end; end; (* Case with menu options*) end; (* End of processMenuSelection *)

James Tam A Sorting Example (19) (***************************************************************** Start of main program *****************************************************************) begin var tamjClientList : ClientList; var clientCount : integer; var menuSelection : char; readClientInformation(tamjClientList, clientCount); displayInstructions; repeat begin displayMenu; readln(menuSelection); processMenuSelection(tamjClientList,clientCount,menuSelection); end; (* repeat-until loop *) until (menuSelection = 'Q') OR (menuSelection = 'q'); saveClientInformation(tamjClientList, clientCount); end. (* End of main program *)

James Tam You Should Now Know How to trace through a program that employs the Bubble Sort How to write the code for a bubble sort to reorder an array