Programming Diary v.0.1

Programming Diary v.0.1

The topic of the Week: Sorting Algorithm

Bubble sort

You are going to try sorting Array (step-by-step).In each step, you will compare all the elements. Propose of each step is to pass the largest to the end of the Array.After each step ignores the last largest element of the Array because it is already sorted, why do we have to check again, that we check before? We will do this n - 1 time, every loop will decrease the length of the Array.

Slection Sort

My first opinion similar sorting method but is not stable, use it when you have a small array or list. Each step looks like you have to find the max element of the Array and swap it with the last position. Maybe you have the opposite opinion? Give me feedback.

Insertion Sort

The main idea of Insertion Sort is to try sorting the array part by part.
For every index put that index element at the correct index in the left part.

Why use it?

  • Stable

  • Adaptive: Steps get reduced array sorted.

Use for smaller values of n → work well when parts of the array are already sorted → It takes part in a hybrid sort algorithm.

When an element j - 1 is smaller than an element j, break the loop. Because the left side is already sorted. i - represents index which we will sort from 0 till n-1

Cycle Sort

I like this sorting algorithm, maybe I will find better one, but right now in cases that I have in my code practice is the best.

Patterns:

  • When you given numbers from range 1 to N → use cyclic sort

  • Question: Find missing number in array ( positive or negative, duplicate)

How it works:

  • You are checking every elements by: index = value - 1;

Next week

My goal on next weak learn and introduction to String ,StringBuilder and Recursion. See you next week. If you have advices or your opinion just share, I’m open to communicating. Keep in touch.