List of Sorting Algorithms
This topic is one of the most common studied. When somebody started preparation…
May 15, 2019
It is one of a simple algorithm to study for a beginner to understanding sorting algorithms.
In this algorithm, we start with 0th index value. We compare it in whole array to find if any other number is smaller than this. So effectively, we find the minimum number through rest of the array from our starting index.
And, we swap the numbers of the initial number and the smalles number found in array.
See the code here:
public void sort(int[] arr) {
int l = arr.length;
for (int i=0; i<l-1; i++) {
int smallestNumIndex = i;
for (int j=i+1; j<l; j++) {
if (arr[smallestNumIndex] > arr[j]) {
smallestNumIndex = j;
}
}
if (i != smallestNumIndex) {
//swap
int temp = arr[i];
arr[i] = arr[smallestNumIndex];
arr[smallestNumIndex] = temp;
}
}
}
The algorithm runs on O(n^2) in worst/average case.
This topic is one of the most common studied. When somebody started preparation…
Problem Statement Given an array, rotate the array to the right by k steps…
Graph Topological Sorting This is a well known problem in graph world…
Problem Statement You are given a string text of words that are placed among…
Max Priority Queue is a data structure which manage a list of keys(values). And…
Min Priority Queue is a data structure which manage a list of keys(values). And…
Introduction In this post we will see following: How to schedule a job on cron…
Introduction There are some cases, where I need another git repository while…
Introduction In this post, we will see how to fetch multiple credentials and…
Introduction I have an automation script, that I want to run on different…
Introduction I had to write a CICD system for one of our project. I had to…
Introduction Java log4j has many ways to initialize and append the desired…