### Longest Common Prefix - Leet Code Solution

Problem Statement Write a function to find the longest common prefix string…

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.

- We start with two loops. Outer loop gives inner loop the index of array to start with.
- Inner array start with the index that outer loop gave.
- Inner loop finds the minimum number
- Swap the indexes, if found the smaller number
- Inner loop breaks, Outer loop move ahead.
- So, we left the smallest number found so far in beginning. And, sort rest of the 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;
}
}
}
```

- Its an in-place sorting algorithm
- Performance is usually worse than Insertion sort
- Applicable for small set of input only
- Its very simple algorithm

The algorithm runs on O(n^2) in worst/average case.

Problem Statement Write a function to find the longest common prefix string…

In this post, we will see some of the frequently used concepts/vocabulary in…

Here are some tips while giving your coding interviews. 1. Never try to jump to…

Problem Statement Given a linked list, swap every two adjacent nodes and return…

Problem Statement Given an array nums of n integers, are there elements a, b, c…

Problem Statement Given two strings s and t , write a function to determine if t…

Introduction Strapi is a backend system provides basic crud operations with…

Introduction I had to create many repositories in an Github organization. I…

Introduction I was trying to download some youtube videos for my kids. As I have…

Introduction In this post, we will explore some useful command line options for…

Introduction In this post, we will see how we can apply a patch to Python and…

Introduction We will introduce a Package Manager for Windows: . In automations…