PROJECT REPORT
ON
SORTING IN BUBBLE, SELECTION AND INSERTION
SUBMITTED BY
Kongpob Chaonan 6081013
Preraphan Srirojanapinyo 6081124
Present
Mingmanas Srivaraksa
Programming Technique
Trimester 2
January 25, 2019
Preface
1
Sorting is an algorithm to arrange the data in proper order. In computer
programming, there are numerous types of sorting to use in C language. The difference
type of sorting can work in different field of work, and it might have some side effects if
the code mostly depend in evaluate order. In this paper, we are focusing on Bubble sort,
Insertion sort, and Selection sort that programming in c language by codeblocks.
Kongpob Chaonan 6081013
Preraphan Srirojanapinyo 6081124
Table of Contents
Contents Page
2
Preface ______________________________________________ 1
Bubble Sort __________________________________________ 3
Explaining of Bubble Sort ______________________________ #
Insertion Sort _________________________________________ #
Selection Sort_________________________________________ #
Type 1 : Bubble Sort
Source Code :
3
Bubble sort data sample :
4
For this code, we try to sorted the numbers from an argument which are:
5,10,15,2,1 and 7 respectively. The line after (“===”) symbol has shown the
process of how the numbers has been sorted for each loop in the algorithm. For
bubble sort, the algorithm will checked for two elements and then calling the
swap function to swap them in they’re in the wrong order until all elements has
been sorted.
At the beginning the input was
5 10 15 2 1 7
5
When we’re running the program, the first loop will find that 15 needs to swap
with 2 because it’s checking if the number in front is more than the next number
or not. This algorithm is used to sorted in ascending order, so the higher
numbers will be swapped to the back. Here the number that has been swapped in
each loop will be shown in highlight.
5 10 2 1 7 15
The swapping will continue as shown in the table below until the numbers are in
the right order.
5 2 1 7 10 15
2 1 5 7 10 15
1 2 5 7 10 15
Type 2 : Insertion Sort
Source Code :
6
Insertion sort data sample :
The number in this argument are 5, 10, 15, 2, 1, and 7. In this type of algorithm,
the insertion sort, is worked by transferred elements one at a time to the
appropriate position. Each element will be checked with the all previous elements
7
if it’s in the right order. This iteration will continue until all elements has been
sorted. We also set the function called j to show the position of the previous
number to check with the value from the position i which contains the value we
need to checked.
At the beginning the number and the value were
The value = 0
My j is = 0 ( since i = 1, so j will start at 0)
5 10 15 2 1 7
When we first execute the code, as we made the value start with a[i], where a = 1
which contain number 10. But when check with j, a[j]=a[0] where j=i-1 which is the
position of 5, so there will be no change here(or break to next loop), the next is
10,15 but it’s already in the order, same as 5,10 so it’ll break to next loop to 2,15
which 15 is more than 2, so it’ll start finding place for insert 2 here :
Now value = 2
My j is = 2 from here it’ll start compared with 15,10 and 5 respectively
5 10 15 2 1 7
After it has compared all numbers, 2 will be at a[0], where i=0 and j=-1 (or first
position) as 2 is less than 1 and 7
5 10 15 15 1 7
8
5 10 10 15 1 7
5 5 10 15 1 7
2 5 10 15 1 7
The loop will continue until the last number is sorted, so in the end we’ll get
1 2 5 7 10 15
9
Type 3 Selection sort :
Source code :
Selection sort data sample :
For selection sort, the elements in this argument are 5,10,15,2,1 and 7. Selection
sort is the algorithm which sorting by finding the elements which highest or
smallest (depends on if it’s ascending or descending order) and then sorted it at
the beginning of the array. In this method, the elements which has been sorted
will not be included when sorted in the next loop anymore because it’s already
decided that the sorted value are already in the highest or lowest location during
that loop.
The progress of the selection sort :
First, we have these unsorted numbers as shown below.
5 10 15 2 1 7
10
After that, it found that the lowest number in the loop is 1, so it swapped position
with the first element which is 5
1 10 15 2 5 7
After that it checked between 10,15,2,5,7 the lowest value here is 2, so the
position between 10,2 will be swapped
1 2 15 10 5 7
The process will be repeated until all element has been sorted, which shown
below
1 2 5 7 10 15
11
The comparison of sorting between Bubble, Insertion, and Selection
The bubble sort is an algorithm that comparing between two arrays by
checked and swapped if they are in wrong order. The process will continue until
the whole series numbers sorted.
The insertion sort is comparing first array and second array, and it will
swap if it is not in position. The process will continue to the third, fourth until the
last array.
The selection sort is also an algorithm, but this sort will find the smallest
number, and it will swap with the first array until right position.