Sorting problem solution 30 days of code HackerRank

Input Format
The first line contains an integer, , denoting the number of elements in array .
The second line contains  space-separated integers describing the respective values of .
Constraints
  • , where .
Output Format
Print the following three lines of output:
  1. Array is sorted in numSwaps swaps.
    where  is the number of swaps that took place.
  2. First Element: firstElement
    where  is the first element in the sorted array.
  3. Last Element: lastElement
    where  is the last element in the sorted array.

Sorting problem solution 30 days of code HackerRank

Problem solution in Java programming language.

import java.util.Scanner;

public class Solution {
    private static int[] array;

    private static void bubbleSort() {
        int n = array.length;
        
        // Number of swaps for all array iterations
        int totalSwaps = 0; 

        for (int i = 0; i < n; i++) {
            // Track if a swap was made
            boolean swapped = false;
            
            for (int j = 0; j < array.length - 1; j++) {
                if (array[j] > array[j + 1]) {
                    int tmp = array[j];
                    array[j] = array[j + 1];
                    
                    array[j + 1] = tmp;
                    swapped = true; 
                    totalSwaps++;
                }
            }

            // Terminate loop as soon as array is sorted
            if (!swapped) {
                break;
            }
        }
        
        // Print answer
        System.out.printf("Array is sorted in %d swaps.\n", totalSwaps);
        System.out.printf("First Element: %d\n", array[0]);
        System.out.printf("Last Element: %d\n", array[n - 1]);
    }
    
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        array = new int[n];
        for (int i = 0; i < n; i++) {
            array[i] = in.nextInt();
        }
        in.close();

        bubbleSort();
    }
}


Second solution

private static void bubbleSort() {
    int n = array.length;

    // number of swaps for all array iterations
    int totalSwaps = 0; 

    for (int i = 0; i < n; i++) {
        // number of swaps for current array iteration
        int numSwaps = 0;

        for (int j = 0; j < array.length - 1; j++) {
            if (array[j] > array[j + 1]) {
                int tmp = array[j];
                array[j] = array[j + 1];
                array[j + 1] = tmp;
                numSwaps++; 
            }
        }

        if (numSwaps == 0) {
            System.out.printf("Array is sorted in %d swaps.\n", totalSwaps);
            System.out.printf("First Element: %d\n", array[0]);
            System.out.printf("Last Element: %d\n", array[n - 1]);

            break;
        }
        else {
            totalSwaps += numSwaps; 
        }
    }
}

Third solution

import java.util.Scanner;

public class Solution {
    private static int[] array;

    private static void bubbleSort() {
        int n = array.length;
        
        // number of swaps for all array iterations
        int totalSwaps = 0; 

        for (int i = 0; i < n; i++) {
            // number of swaps for current array iteration
            int numSwaps = 0;
            
            for (int j = 0; j < array.length - 1; j++) {
                if (array[j] > array[j + 1]) {
                    int tmp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = tmp;
                    numSwaps++; 
                    totalSwaps++;
                }
            }

            if (numSwaps == 0) {
                System.out.printf("Array is sorted in %d swaps.\n", totalSwaps);
                System.out.printf("First Element: %d\n", array[0]);
                System.out.printf("Last Element: %d\n", array[n - 1]);
                
                break;
            }
        }
    }
    
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        array = new int[n];
        for (int i = 0; i < n; i++) {
            array[i] = in.nextInt();
        }
        in.close();

        bubbleSort();
    }
}


Problem solution in Python programming language.

import sys

n = int(input())
a = [int(i) for i in input().strip().split(' ')]
numSwaps = 0

for i in range(n):
    currentSwaps = 0

    for j in range(0, n - 1):
        if a[j] > a[j + 1]:
            a[j], a[j + 1] = a[j + 1], a[j]
            currentSwaps += 1
            numSwaps += 1
            
    if currentSwaps == 0:
        break
        
print("Array is sorted in " + str(numSwaps) + " swaps.")
print("First Element: " + str(a[0]))
print("Last Element: " + str(a[n - 1]))

Post a Comment

0 Comments