0% found this document useful (0 votes)
21 views

Simple Sorting Algorithms

This document describes three simple sorting algorithms: insertion sort, selection sort, and bubble sort. Insertion sort works by inserting each item into its proper place in the final sorted list. Selection sort finds the smallest element and exchanges it with the first element, then finds the next smallest and exchanges it with the second element, and so on. Bubble sort scans the list from bottom to top and swaps adjacent elements that are out of order until the list is fully sorted. All three algorithms have O(n^2) time complexity and operate in-place without using additional memory.

Uploaded by

Abdi Negassa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Simple Sorting Algorithms

This document describes three simple sorting algorithms: insertion sort, selection sort, and bubble sort. Insertion sort works by inserting each item into its proper place in the final sorted list. Selection sort finds the smallest element and exchanges it with the first element, then finds the next smallest and exchanges it with the second element, and so on. Bubble sort scans the list from bottom to top and swaps adjacent elements that are out of order until the list is fully sorted. All three algorithms have O(n^2) time complexity and operate in-place without using additional memory.

Uploaded by

Abdi Negassa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Chapter Two

Simple Sorting Algorithms


Simple Sorting Algorithms

• Sorting is one of the most important operations performed by


computers.
• Sorting is a process of reordering a list of items in either increasing
or decreasing order.
• The following are simple sorting algorithms used to sort small-sized
lists.
– Insertion Sort
– Selection Sort
– Bubble Sort
Insertion Sort
• The insertion sort works just like its name suggests - it inserts each
item into its proper place in the final list.
• The simplest implementation of this requires two list structures -
the source list and the list into which sorted items are inserted.
• To save memory, most implementations use an in-place sort that
works by moving the current item past the already sorted items and
repeatedly swapping it with the preceding item until it is in place.
• It's the most instinctive type of sorting algorithm. The approach is
the same approach that you use for sorting a set of cards in your
hand.
Insertion Sort
Basic Idea:
• Find the location for an element and move all others up, and insert
the element.
The process involved in insertion sort is as follows:
1. The left most value can be said to be sorted relative to itself.
Thus, we don’t need to do anything.
2. Check to see if the second value is smaller than the first one. If it
is, swap these two values. The first two values are now relatively
sorted.
3. Next, we need to insert the third value in to the relatively sorted
portion so that after insertion, the portion will still be relatively
sorted.
4. Remove the third value first. Slide the second value to make
room for insertion. Insert the value in the appropriate position.
5. Now the first three are relatively sorted.
6. Do the same for the remaining items in the list.
Contd.
Implementation
void insertion_sort(int list[]){
int temp;
for(int i=1;i<n;i++) Analysis
{ How many comparisons?
temp=list[i]; 1+2+3+…+(n-1)= O(n2)
How many swaps?
for(int j=i; j>0 && temp<list[j-1];j--)
1+2+3+…+(n-1)= O(n2)
{ // work backwards through the array How much space?
// finding where temp should go In-place algorithm
list[j]=list[j-1];
list[j-1]=temp;
} //end of inner loop
} //end of outer loop
} //end of insertion sort
Selection Sort
 Selection sort is an attempt to localize the exchanges of array elements

by finding a misplaced element first and putting it in its final place.

 The element with the lowest value is selected and exchanged with the

element in the first position. Then, the smallest value among the

remaining elements List[1], . . . , List[n-1] is found and put in the

second position.

 This selection and placement by finding, in each pass i, the lowest

value among the elements List[i], . . . , List[n-1] and swapping it with

List[i] are continued until all elements are in their proper positions
Selection Sort
Basic Idea:

Loop through the array from i=0 to n-1

Select the smallest element in the array from i to n

Swap this value with value at position i.


Implementation: Contd.
void selection_sort(int list[])
{
int i,j, smallest;
for(i=0;i<n;i++){
smallest=i; Analysis
for(j=i+1;j<n;j++){
How many comparisons?
if(list[j]<list[smallest])
(n-1)+(n-2)+…+1= O(n2)
smallest=j;
How many swaps?
}//end of inner loop
n=O(n)
temp=list[smallest];
How much space?
list[smallest]=list[i];
In-place algorithm
list[i]=temp;
} //end of outer loop
}//end of selection_sort
Bubble Sort
• A bubble sort can be best understood if the array to be sorted is
envisaged as a vertical column whose smallest elements are at the top
and whose largest elements are at the bottom.
• The array is scanned from the bottom up, and two adjacent elements are
interchanged if they are found to be out of order with respect to each
other.
• First, items List[n-1] and data[n-2] are compared and swapped if they are
out of order.
• Next, List[n-2] and List [n-3] are compared, and their order is changed
if necessary, and so on up to List [1] and List [0].
• In this way, the smallest element is bubbled up to the top of the array
Bubble Sort

• Bubble sort is the simplest algorithm to implement and the slowest


algorithm on very large inputs.

Basic Idea:
Loop through array from i=0 to n and swap adjacent elements if they are
out of order.
 Implementation:

void bubble_sort(list[])
{ Analysis of Bubble Sort
int i,j,temp;
How many comparisons?
for(i=0;i<n; i++){
(n-1)+(n-2)+…+1= O(n2)
for(j=n-1;j>i; j--){
How many swaps?
if(list[j]<list[j-1]){
(n-1)+(n-2)+…+1= O(n2)
temp=list[j];
Space?
list[j]=list[j-1];
In-place algorithm.
list[j-1]=temp;
}//swap adjacent elements
}//end of inner loop
}//end of outer loop
}//end of bubble_sort

You might also like