Simple Sorting Algorithms

Implementation of sorting algorithms

  1. Bubble Sort

1.PNG

2. Selection Sort

2.PNG

3. Insertion Sort

12.PNG

Out of these three algorithm which one is better? How can you measure this?

Important influence factors to the performance of a sorting algorithm can be separated into two main parts:

  • Memory Consumption of the algorithm
  • Total runtime of an algorithm

 

In case the program is to some case a program which interacts with others also the response time is a very important fact of the performance of a program.

How to measure Memory Consumption?

The total used / free memory of an program can be obtained in the program via java.lang.Runtime.getRuntime(); The runtime has several met

3.PNG

hod which relates to the memory. The following code example demonstrate its usage.

 

 

 

 

 

  • Calculate memory consumption for above sorting algorithms using above function for 100 arrays.

4.PNG

 

 

 

 

 

 

 

 

 

  • Bubble sort algorithm has the minimum number of standard deviation

5.PNG

  • But Insertion sort algorithm has the minimum memory usage. (298.98KB)
  • Difference of standard deviation between bubble and insertion has 9.452, but average has 136. So, insertion algorithm is good for memory consumption

6.PNG

Performance of algorithm according to memory usage

From https://en.wikipedia.org/wiki/Sorting_algorithm

7.PNG

How to measure runtime of an algorithm?

Use System.nanoTime(); to get the start time and the end time and calculate the difference.8.PNG

  • Calculate run-time for above sorting algorithms using above function for 100 arrays.

9.PNG

 

Summary

10.PNG

Conclusion

The response time is a very important fact of the performance of a program. So, insertion algorithm has the minimum run time.

Considering runtime and memory consumption, insertion algorithm is the best sorting algorithm.

 

Performance of algorithm according to response time

From https://en.wikipedia.org/wiki/Sorting_algorithm

11.PNG

 

Published by

Unknown's avatar

shehan Shaman Mario Perera

B.Sc. Engineering undergraduate in computer Engineering at University of Peradeniya Sri Lanka

Leave a comment