hvn-network

My Blog List

Monday, August 22, 2016

Sorting and Searching Algorithms

created by Frank


1. Sorting 

in this section, we will compare the performance of basic sorting algorithms: bubble sort, selection sort, insertion sort, quick sort, and heap sort.

  • in these algorithms, quick sort and heap sort are fastest but hardest to implement. (quick sort is usually fastest)
  • bubble sort is simplest but the its performance is lowest 
about the complexity of algorithms:
  • bubble sort: O(n2)
  • insertion sort: O(n2)
  • selection sort: O(n2)
  • quick sort: average is O(nlogn), worst case is O(n2)
  • heap sort: O(nlogn)
because these algorithms are common, we do not mention their principle here
below is source code of sort algorithms

1.1 bubble sort

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

1.2 insertion sort


#include 
#include 
#include 

1.3 selection sort

#include 
#include 
#include 

1.4 quick sort

#include 
#include 
#include 

1.5 heap sort

#include 
#include 
#include 
for simple, we use rand() function to generate input data of programs. the number of test case is 5 and the result is average of them.
download input data
The system for implementation: 
  • Intel core I7-6820HQ CPU 2.7 GHz
  • OS ubuntu 12.04 64bit
  • Compiler: gcc ubuntu/linaro 4.6.3
See run time of these algorithms, we can see the dramatic difference among algorithms

bubble
Selection
Insertion
Quick
Heap
Test case 0
30.30
9.38
6.01
0.02
0.03
Test case 1
30.39
9.39
5.99
0.01
0.03
Test case 2
30.41
9.48
5.98
0.01
0.02
Test case 3
30.38
9.43
5.95
0.01
0.03
Test case 4
30.74
9.73
5.98
0.01
0.03
Test case 5
30.38
9.55
6.08
0.01
0.03
Average
30.4333
9.4933
5.9983
0.0133
0.0283

download input data here

2. Searching 

in this section, we will review 2 common searching: sequence searching and binary searching. 
  • sequence searching: 

References

No comments:

Post a Comment