You are on page 1of 33

CO1401

Programming

Week 8
Sorting & Strings
Topics

• Insertion Sort
• Strings

2 Programming
Algorithms

3 Programming
Algorithms

4 Programming
Sorting

• Algorithms are a central element of programming.


• This week I want to look at sorting.
• Searching and sorting are interconnected.
• I believe that a number of you are familiar with "bubble
sort".
• A bubble sort sorts the numbers by going through the
array and letting the biggest number "bubble" to the top
of the array.
• We then ignore this top element (it's already sorted) and
instead only consider the rest of the array. We let the
biggest number in this part "bubble" to the top.
• This process is repeated until we run out of array.
5 Programming
insertion sort

• An insertion sort is one of the simplest sorting


algorithms but it's reasonably efficient.

• The sort will arrange the numbers into order by


inserting an unsorted item into a sorted list. The item
being inserted into the correct place so that the list
remains sorted.

6 Programming
insertion sort

• Akin to sorting a hand of playing cards.


• I will hold a fan of the cards in my right hand.
• I take one card out of right hand and place it in my left
hand.
• When you select a card, you do this done at random.
You just pick one. In practice the easiest way to
implement this will be take the leftmost card in the
fan.
• Having taken one card out of my right hand I place it
in the correct position in my left hand.

7 Programming
insertion sort

• I keep on doing this until I run out of cards.


• The cards in my left hand will be sorted.

• For example, in my right hand I have a 7, 5 and 9.


• I put the 7 in my left hand.
• I take the next card, which is the 5.
• I put the 5 in my left hand. It will be placed before the 7.
• I take the next card: which is the 9.
• I put the 9 in my left hand. It will be placed after the 7.
• No more cards, and the cars in my left hand are sorted.

8 Programming
insertion sort

• Obviously the "place it in the correct position in my


left hand" bit of the algorithm needs more
explanation.
• From the description it probably sounds like we're
going to use two arrays. In practice, the sorting is
typically done in-place, i.e. we only use one array.
• We can effectively partition the array into a sorted
part and an unsorted part. We need to remember the
index of the partition. This index gives the end of end
location of the first half, and the start location of the
second half.

9 Programming
insertion sort

• For example, the sorted part would be from the start


of the array to the partition index.
• The unsorted part would be from partition index to
the end of the array.
• In each pass of the algorithm we'll move one element
from the unsorted part of the array to the sorted part
of the array.
• The partition index will increase by one as we do this.

10 Programming
insertion sort

• The algorithm uses a search.


• The way I've expressed the algorithm is that it
includes the step: "place it in the correct position in
my left hand".
• If the cards in my left hand are sorted then all that is
required is a search.
• I search through until I find the right location and then
insert the card.
• This is why it is called an insertion sort.

11 Programming
Insertion sort

12 Programming
Insertion sort

13 Programming
Insertion Sort

• 6 3 1 9 4 12 17 2
• 3 6 1 9 4 12 17 2
• 1 3 6 9 4 12 17 2
• 1 3 6 9 4 12 17 2
• 1 3 4 6 9 12 17 2
• 1 3 4 6 9 12 17 2
• 1 3 4 6 9 12 17 2
• 1 2 3 4 6 9 12 17

14 Programming
Insertion Sort

• Sorting in ascending order.

• The first element placed into the sorted array is


chosen at random. Obviously an array containing one
element is, by definition, sorted.
• The easiest thing to do is to just use the leftmost
element on the array as our first element.

15 Programming
Insertion Sort - the algorithm

• The algorithm then runs an outer loop from an index


position of 1.
• The outermost loop of the code moves progressively
through the array until it reaches the end, i.e. through the
unsorted array. In each loop it selects the next value to be
inserted.

• We also use an inner loop. The inner loop starts from the
highest (right hand side) of the sorted array. It "slides" the
numbers across.
• The loop progressively goes through the sorted array until
it find the correct place to put the value to be inserted.

16 Programming
Insertion Sort - code
void insertionSort(int numbers[], int arraySize)
{
int i, j, insert;

for (i = 1; i < arraySize; i++)


{
insert = numbers[i];
j = i;
while ((j > 0) && (numbers[j - 1] > insert))
{
numbers[j] = numbers[j - 1];
j = j - 1;
}
numbers[j] = insert;
}
}
17 Programming
Insertion Sort - variants

• The version I've provided "slides" the elements of the


array across.
• Need to search through the values in the sorted array.
• Could employ a bubble sort.
• Could employ a binary search (the one we did last
week.) To be formal, this variant would be called a
binary insertion sort.

18 Programming
Insertion Sort - efficiency

19 Programming
Insertion Sort - implementation efficiency

• Algorithms can be judged by efficiency:


• Speed
• Memory
• By sorting in place this algorithm is efficient with
respect to memory.
• The sorting is done in-place. Uses the array itself. It
does this by carefully partitioning off the array into an
unsorted and a sorted part. This is a common approach
used in a number of implementations of sorting. It is an
efficient algorithm.
• Typically your sort is going to be in a function.
• Arrays are passed over to functions by reference.

20 Programming
Insertion Sort - implementation efficiency

• It would cost time and memory to use, for example, a


method which employs a temporary array and then
copies values back. We're directly manipulating the
original array. So an efficient implementation.

21 Programming
Topic 2

• Strings

22 Programming
Strings

• Useful to look at strings in the context of C++.


• Strings and string manipulation are also of direct
relevance to your assignment.

• "string" is a C++ class.


• Classes combine data and the methods associated
with the data. The methods are operations which we
use with the data.
• String class contains the characters which make up
the string.

23 Programming
Strings

• Each string object contains the characters in the


string.
• It also holds the size of the string.
• There are a number of useful methods.
• The methods a invoked by using the dot operator.
• Advanced: Note that the dot operator is the same
operator that you used with structures. Although they
have different names, structures and classes are
virtually identical (expect in one respect) - it is exactly
the same dot operator being used.

24 Programming
Strings

#include <iostream>
#include <string>
using namespace std;

int main()
{
string str;
cout << str.length();
}

25 Programming
String operators: length

• Need to use the string library to access the string


operator (notes that iostream on its own gives access
to string itself)
• Returns length of the string (size is exactly equivalent).

cout << str.length();

26 Programming
String operators: compare

• compare
• Compares two strings. Note that the comparison uses
the ASCII coding, so this provides only a limited ability
to alphabetical sorting

cout << str.compare( str1 );

• 0 if same
• -1 if alphabetically smaller
• 1 if alphabetically larger

27 Programming
String operators

• getline: reads a string until the newline character is met.


You provide the input stream. Could be a file input
stream.
getline( cin, str );

• concatenation (joining)
str = str1 + str2;

• empty: tests if the string is empty


if( str.empty() )

28 Programming
String operators - find

• Find returns the index of the beginning of the search


string. Finds the first occurrence.

• Returns the index of the string if found.


• Returns string::npos if the substring is not found.

• npos is a term defined in the <string> library.


• It is declared as being of type size_t

29 Programming
String operators - size_t

• size_t is a commonly used data type so I would like to


talk about it here.
• size_t is a platform independent data type. It could be
defined differently on different computers.
• In practice: unsigned integer.
• An unsigned integer is 4 bytes ( can be 64 bits on
some target platforms)
• If you test npos you will find it has a value of -1.
• -1 is actually the maximum value for size_t
• If you output the value of npos it will be 4294967295
• This is actually the same as -1 for an unsigned integer.
30 Programming
String operators - find

31 Programming
String operators - replace

• Replaces a section of the string by some other contents


string str = "hello";
string sub= "world";
str.replace( 1, 2, "--");
cout << str << endl; // h--lo
• The first number is the position within the string of the
first character of the section to be replaced.
• The second number is the length of the section to be
replaced.
• The characters are the replacement string - could be a
string.
32 Programming
String operators - substr

• Returns a substring from a string.


• The substring is a copy of the characters starting from
the position given in the parameter list - the first
number.
• The second number is the length of the substring.
string str = "hello";
string sub;
sub = str.substr( 2, 3 );

cout << sub << endl; // llo

33 Programming

You might also like