selection sort - Searching and sorting Reasons for not...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Lecture 1 -- 1 Computer Science I - Martin Hardwick Searching and sorting Reasons for not using the most efficient algorithm include: The more efficient algorithm is much more complicated.  Therefore, it is harder to maintain and debug. The more efficient algorithm requires another part of the  system to prepare data and preparing this data takes a long  time. For example, binary search requires sorted data As we shall see the sorting algorithms are relatively  complicated. And they are slow – the best algorithms are O (n log n). Making searching faster by going from O (n) to O (log n) is not  worth it if the price is having to use a new algorithm that is O(n  log n). However, society has organized itself so that sorting does not have to be used too often For example, banks only sort their data once per day
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Lecture 1 -- 2 Computer Science I - Martin Hardwick Creating a Sorted List void banking::Insert( /* in */ acct item ) // Inserts into a list of bank accounts // Data is vector <acct> { int index = data.size() - 1; data.push_back (item); while (index >= 0 && item.get_num() < data[index].get_num()) { data[index+1] = data[index]; index--; } data[index+1] = item; // Insert item } As we have seen, using the Insert() operation for the SortedList class repeatedly allows us to create a sorted list. but is this efficient? In the worst case, each call to Insert() moves the entire list one slot. first call moves 0 items second call moves 1 item third call moves 2 items  . . . Nth call moves N-1 items. For a list of length N, this requires: 0 + 1 + 2 + … + (n-1) moves ) ( 2 1 2 1 2 ) 1 ( 2 2 n O n n n n = - = -
Background image of page 2
Lecture 1 -- 3 Computer Science I - Martin Hardwick Discussion Repeated use of the Insert() operation sorts a list using O(n 2 ) moves and O(n 2 ) comparisons. each move is preceded by a comparison between 
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 13

selection sort - Searching and sorting Reasons for not...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online