Compare Sorting Algorithms in C Language
Compare Sorting Algorithms in C Language
ON
SORTING IN BUBBLE, SELECTION AND INSERTION
SUBMITTED BY
Present
Mingmanas Srivaraksa
Programming Technique
Trimester 2
Preface
1
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,
Table of Contents
Contents Page
2
Preface ______________________________________________ 1
Selection Sort_________________________________________ #
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.
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
numbers will be swapped to the back. Here the number that has been swapped in
5 10 2 1 7 15
The swapping will continue as shown in the table below until the numbers are in
5 2 1 7 10 15
2 1 5 7 10 15
1 2 5 7 10 15
Source Code :
6
The number in this argument are 5, 10, 15, 2, 1, and 7. In this type of algorithm,
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.
The value = 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
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
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
Source code :
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
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.
5 10 15 2 1 7
10
After that, it found that the lowest number in the loop is 1, so it swapped position
1 10 15 2 5 7
After that it checked between 10,15,2,5,7 the lowest value here is 2, so the
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
checked and swapped if they are in wrong order. The process will continue until
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.