Docsity

Assignment 7: Sorting - Data Structures and Algorithms | MIS 301, Assignments of Business Management and Analysis

Toggle sidebar
bg1
SORTING MIS 121
Sorting
•Major activity of computers
•Major topic in MIS 301
•Over a hundred sorting algorithms
•Classic text is Donald Knuth, Sorting and Searching, Second Edition, Addison Wesley,
1998. (Volume 3 of Art of Computer Programming) see
www-cs-faculty.stanford.edu/∼knuth/index.html1.
Sorting Algorithms
•Two kinds of sorting algorithms (from standpoint of MIS 301 student)
— basic algorithms (easy to understand but slow)
— efficient algorithms (faster, harder to explain)2.
Educational Challenge
•Major problem in computer education is to describe sorting algorithms so they can be un-
derstood and you can use them
•Usual solution is to first show basic algorithms ... but then students wind up using them
with disastrous results3.
Basic and Efficient Algorithms
•Basic
— insertion
— selection
— bubblesort (the worst of all! never use it!)4.
Basic and Efficient Algorithms—2
•Efficient
— ShellSort
— HeapSort
— QuickSort5.
Sorting Times for 16,000 Integers
SORTING Printed 12 November 1998, Page 1
pf3

Partial preview of the text

Download Assignment 7: Sorting - Data Structures and Algorithms | MIS 301 and more Assignments Business Management and Analysis in PDF only on Docsity!

Sorting

  • Major activity of computers
  • Major topic in MIS 301
  • Over a hundred sorting algorithms
  • Classic text is Donald Knuth, Sorting and Searching , Second Edition, Addison Wesley,
    1. (Volume 3 of Art of Computer Programming ) see 1. www-cs-faculty.stanford.edu/∼knuth/index.html

Sorting Algorithms

  • Two kinds of sorting algorithms (from standpoint of MIS 301 student) — basic algorithms (easy to understand but slow) 2. — efficient algorithms (faster, harder to explain)

Educational Challenge

  • Major problem in computer education is to describe sorting algorithms so they can be un- derstood and you can use them 3.^ •^ Usual solution is to first show basic algorithmswith disastrous results^...^ but then students wind up using them

Basic and Efficient Algorithms

  • Basic — insertion — selection 4. — bubblesort (the worst of all! never use it!)

Basic and Efficient Algorithms—

  • Efficient — ShellSort — HeapSort 5. — QuickSort

Sorting Times for 16,000 Integers

Sorting Order Prior to Sort Method Ascending^ Random^ Descending insertion 0.02 63.63 124. selectionBubblesort 88.9885.61 (^) 172.2893.38 (^) 254.5290. ShellSort 0.20 0.67 0. HeapSort 0.85 0.77 0. QuickSort 0.22 0.35 0.

6. source: Drozdek & Simon, Data Structures in C , PWS, 1995, page 351. All times in seconds. Why I’m telling you all this

  • Our textbook shows selection sort in §9.
  • You can use it for hw7 (it’s in sorting1.c and sorting2.c)
  • You should know its place as an algorithm that is easy to write but not very useful for sort- 7. ing large amounts of data Selection Sort Algorithm for(i=0;i<n-1;i++) { select the smallest item from among data[i], ... , data[n-1] swap it with data[i] } 8. (note: this is how you’ll get algorithms in MIS301, then you translate into code) Selection Sort Algorithm in More Detail for(i=0;i<n-1;i++) { for(j=i+1,least=i;j<n;j++) if (data[j] < data[least]) least=j; swap(data[least],data[i]); 9. } Comma Expressions
  • It’s possible to do more than one initialization and increment in the head of a for loop.
  • Use comma expression to do it.
  • Example: 10. for (x=0,y=N;x<N&&y>0;x++,y--) Comma Expression Definition
  • Two expressions separated by a comma.
  • Evaluation: