11 views

Uploaded by June Ng

save

- Searching and Sorting
- Projec29000
- Cpds Previous Papers Questions 2012 Harsha
- [2016] PGEE - Interview Experience
- Sorting Algorithms-Sesion 6
- L01
- Sorting 2
- Soa
- ADA File
- CSC108_Final_2009F.pdf
- An Empirical Study on Attainment of Course Outcome for an Engineering course – Case study of Data Structures.
- DAA Report - simulator
- Sorting
- Parallel Performance of MPI Sorting Algorithms on Dual-Core Processor Windows-Based Systems
- LessonplanCSE311_withassesmentmethods
- merge_sorted
- hcl(1)
- Lab1
- L06IndexConstruction
- Handl 2002
- CS C363
- 10.1.1.159.8217
- Heap Quick Comparison
- Placement papers
- 07 Sorting Algorithms
- 7
- Fifth
- ACC UT-I
- External Sorting Query
- Quiz1 Sorting
- Struktur Program DPLI (3 Semester)
- Borang_BR1M
- Multiplication Handouts
- GJBC
- jawatan kosong SEAMEO
- Intipati_Pelaksanaan_SBPA
- D__PDFCreated_OUM_Topic10_HBMT3403-255

You are on page 1of 4

1.3. Sorting algorithms 1.4. Searching algorithms

Specification Be able to develop and adapt simple algorithms (Sorting: Bubble, Shuttle, insertion, quick sort. Candidates not expected to memorise these) (A2) Learning objectives Be thoroughly familiar with the sorting algorithms Be thoroughly familiar with the searching algorithms (although not explicitly in specification) Ch.1 p.15-24 Ex.1E Q10,11,12,13,15,16 Multiple Choice Test 3

1.3. Sorting algorithms The aim here is to teach a computer how to sort a list of data, e.g. numbers. The efficiency of the algorithm is very important. (a) Interchange sort algorithm 1. Swap the smallest number with the first number. 2. Find the smallest number excluding the first. Swap this with the second number. 3. Repeat this until the list is sorted. A measure of the efficiency of a sorting algorithm is to count the number of comparisons and swaps needed to sort the data. Use Allsorts to count comparisons and swaps for: • a random list of size 6; • a random list of size 8; • a list in reverse order. What do we notice? • The number of comparisons is independent of the list chosen; •

• 1 n ( n − 1) ; 2 The number of swaps is dependent on the original list: if it is in reverse order, n – 1 swaps are required (one per pass) which is the worst case. So the maximum number of 1 comparisons and swaps is n ( n − 1) + ( n − 1) , and the algorithm therefore has 2 quadratic complexity.

The no. of comparisons for a list of size n is the (n – 1)th triangle number:

(b) Bubble sort algorithm This depends on successive comparisons of pairs of numbers. 1. Compare the 1st & 2nd numbers and swap if necessary (putting the smaller on top). 2. Compare the 2nd & 3rd numbers and swap if necessary. 3. Complete the first pass (the largest number will now be at the bottom). 4. Repeat the steps above, but exclude the last number. 5. Repeat until there are no swaps in a given pass. Use Allsorts (Bubble Down) to count comparisons and swaps for: • a random list of size 6; a list in the correct order; • • a list in reverse order.

What do we notice? • This time the number of comparisons depends on the list; • The worst case is a list in reverse order; 1 • The no. of comparisons in this case is again n ( n − 1) ; 2 1 The no. of swaps in this case is n ( n − 1) • 2 This algorithm therefore also has quadratic complexity. Also we could use this MathsNet demonstration. A disadvantage of bubble sort is that once the data has been sorted, another complete pass through the data is required to ensure that the sorting has been finished. This is partly overcome by: (c) Shuttle sort algorithm 1. Compare the first two numbers and swap if necessary to place in ascending order. 2. Compare the 2nd & 3rd numbers and swap if necessary, then compare 1st & 2nd and swap if necessary (OCR specification: IF 2nd & 3rd swapped) 3. Repeat including the 3rd & 4th numbers, etc. The Allsorts spreadsheet uses the OCR definition. Use it to count comparisons and swaps for: • a random list of size 6; • a list in the correct order; • a list in reverse order. What do we notice? • This time the number of comparisons depends on the list; • The worst case is a list in reverse order; 1 • The no. of comparisons in this case is again n ( n − 1) ; 2 1 • The no. of swaps in this case is n ( n − 1) 2 This algorithm therefore also has quadratic complexity. (d) Insertion sort algorithm This is what you would do to sort a hand of cards. 1. Write down the first number on the list. 2. Write the next number before the first if it is lower, or after it if it is greater. 3. Take the next number and compare it with the first in your list; write before it if it is less. If not compare to the next number, etc. 4. Repeat until all the numbers in the original list have been placed. Use the Allsorts spreadsheet to sort: • a random list of size 6; • a list in the correct order; • a list in reverse order. What do we notice?

The best case here is when the original list is in reverse order, because at each stage we only need to compare the new number with the first on the new list; • The worst case is when the original list is in the correct order! 1 • The no. of comparisons in this case is again n ( n − 1) ; 2 There are no swaps. • Again, the algorithm has quadratic complexity. (e) Quick sort algorithm 1. Split the list into two sublists, one containing numbers less than or equal to the first in the list, the other containing those greater. 2. Repeat the above on sublists containing two or more numbers until no more sublists remain. Use the Allsorts spreadsheet to sort: • a random list of size 6; • a random list of size 8; • a list in the correct order; a list in reverse order. • What do we notice? • Lists in reverse or correct orders are the worst cases, because they generate the most sublists; 1 • The no. of comparisons in this case is again n ( n − 1) ; 2 • There are no swaps. Again, the algorithm has quadratic complexity. Also we could use this MathsNet demonstration. 1.4. Searching algorithms This is not explicitly in the specification: just read through p.22-24, noting the three methods (linear search, indexed sequential search, binary search). There is a demonstration of the binary search algorithm on MathsNet. Resources • The Allsorts spreadsheet is excellent • “Bubble Sort Example” PowerPoint illustrates this algorithm • “QuickSort” PowerPoint illustrates this algorithm • “Binary Search” PowerPoint illustrates this algorithm

•

**ALGORITHMS 1.3. Sorting algorithms
**

Download the Allsorts spreadsheet from the MEI Distance Learning website. (a) 1. 2. 3. Interchange sort algorithm Swap the smallest number with the first number. Find the smallest number excluding the first. Swap this with the second number. Repeat this until the list is sorted.

(b) Bubble sort algorithm This depends on successive comparisons of pairs of numbers. 1. Compare the 1st & 2nd numbers and swap if necessary (putting the smaller on top). 2. Compare the 2nd & 3rd numbers and swap if necessary. 3. Complete the first pass (the largest number will now be at the bottom). 4. Repeat the steps above, but exclude the last number. 5. Repeat until there are no swaps in a given pass. http://www.mathsnet.net/asa2/2004/d11bubble.html (c) Shuttle sort algorithm 1. Compare the first two numbers and swap if necessary to place in ascending order. 2. Compare the 2nd & 3rd numbers and swap if necessary, then compare 1st & 2nd and swap if necessary (OCR specification: IF 2nd & 3rd swapped) 3. Repeat including the 3rd & 4th numbers, etc. (The Allsorts spreadsheet here uses the OCR definition.) (d) Insertion sort algorithm This is what you would do to sort a hand of cards. 1. Write down the first number on the list. 2. Write the next number before the first if it is lower, or after it if it is greater. 3. Take the next number and compare it with the first in your list; write before it if it is less. If not compare to the next number, etc. 4. Repeat until all the numbers in the original list have been placed. (e) Quick sort algorithm 1. Split the list into two sublists, one containing numbers less than or equal to the first in the list, the other containing those greater. 2. Repeat the above on sublists containing two or more numbers until no more sublists remain. http://www.mathsnet.net/asa2/2004/d11quicksort.html

Use the five sorting algorithms and the Allsorts spreadsheet to sort: • a random list of size 6; • a random list of size 8; • a list in the correct order; • a list in reverse order. For each algorithm, try to find which lists produce the best and worst cases.

- Searching and SortingUploaded byMinusree
- Projec29000Uploaded byjayarajmevlsi
- Cpds Previous Papers Questions 2012 HarshaUploaded byHarsha Vardhan
- [2016] PGEE - Interview ExperienceUploaded byTarun Pandey
- Sorting Algorithms-Sesion 6Uploaded byLuigi Garcia Cueva
- L01Uploaded byRonak Chaudhary
- Sorting 2Uploaded byWilLy Libres-de la Cerna
- SoaUploaded byAfeefa
- ADA FileUploaded byjayant777_13
- CSC108_Final_2009F.pdfUploaded byexamkiller
- An Empirical Study on Attainment of Course Outcome for an Engineering course – Case study of Data Structures.Uploaded byIOSRjournal
- DAA Report - simulatorUploaded byNawanjana Hagarangoda
- SortingUploaded byPushkarajJ6
- Parallel Performance of MPI Sorting Algorithms on Dual-Core Processor Windows-Based SystemsUploaded byijdps
- LessonplanCSE311_withassesmentmethodsUploaded byLiNu
- merge_sortedUploaded byipjeaanvbnr
- hcl(1)Uploaded bynamandeep
- Lab1Uploaded bypranay
- L06IndexConstructionUploaded bycecsdistancelab
- Handl 2002Uploaded bys_cynuf
- CS C363Uploaded byAravind Kondamudi
- 10.1.1.159.8217Uploaded byjimakosjp
- Heap Quick ComparisonUploaded byManoj Kumar G
- Placement papersUploaded byKranthi Kumar
- 07 Sorting AlgorithmsUploaded byits4krishna3776
- 7Uploaded byMangat Singh
- FifthUploaded byGagan Bansal
- ACC UT-IUploaded byabhiraj1234
- External Sorting QueryUploaded byEli_Hux
- Quiz1 SortingUploaded byhettiarachchigls