# Lazy sorting hackerrank solution in python

## Introduction

Lazy Sorting is a sorting algorithm that sorts an array by repeatedly finding the minimum element and swapping it with the first element of the unsorted part of the array. The algorithm maintains two subarrays in a given array.

The first sub-array is the sorted part which is initially empty. The second sub-array is the unsorted part which contains all the remaining elements in the array.

## Expansion: At each step, the minimum element from the unsorted array is picked and swapped with the first element of the unsorted array. Then, both sorted and unsorted arrays are updated accordingly. This process continues until there are no more elements left in the unsorted array.

The following illustration will make things clearer:

lazy_sorting(arr) // sort arr lazily
min_index = find_min(arr[1..n]) // find index of min element in arr[1..n]

if min_index != 1 then // if min element is not at arr[1]
swap(arr[1], arr[min_index]) // then swap it with arr[1], i.e., place min elem at arr[1]

sorted_array = [arr[1]] // update sorted and unsorted arrays
unsorted_array = arr[2..n]

for i = 2 to n do // do for remaining elements in arr[]

``````min_index = find_min(unsorted_array)     // find index of min element in unsorted_array

if min_index != 0 then                  // if found, i.e., if min element exists

swap(unsorted_array[0],             // swap it with first elem in Unsroted Arr[]
unsorted_array[min_index])

You are given N distinct integers. You want to sort these integers in increasing order, but you are too lazy to do it yourself. So you ask your friend to help you. He writes down a program that sorts the numbers, but he makes a small mistake: instead of sorting the numbers in increasing order, his program sorts them in decreasing order.

You realize that rather than fixing your friend’s program, you can write a new one that sorts the numbers correctly.

## Your task is simply to find the error in your friend’s program and correct it. To sort the array A in ascending order, write:sort(A).

You may copy your friend’s program verbatim into your source code file; there is no need to change it before you correct it.

## The Solution

def lazy_sorting(arr):
n = len(arr)
mini = min(arr)
maxi = max(arr)

``````ans = 0
for i in range (n):
if arr[i] != mini and arr[i] != maxi:
ans += 1

return ans</p><br /><h2>Implementation</h2><br /><p>``````

def lazy_sorting(arr):
n = len(arr)
even_count = 0
for i in range(n):
if (arr[i] % 2 == 0):
even_count += 1
if (even_count == 0 or even_count == n):
return “YES”
else:
return “NO”

## Results

1. Sort the list in ascending order.
2. Take the difference between each adjacent elements in the sorted list and store it in another list(say diff).
3. Sort the diff list.
4. If the first element of diff is equal to 0, means all the adjacent elements are same in sorted list, hence return 0.
5. Otherwise return the last element of diff(maximum value).
Conclusion
In conclusion, the lazy sorting algorithm is a quick and easy way to sort a list of numbers. It is not the most efficient method, but it is fast and easy to understand.