Day 20: Sorting

Day 20: Sorting

Objective

Today, we’re discussing a simple sorting algorithm called Bubble Sort. Check out the Tutorial tab for learning materials and an instructional video!


Consider the following version of Bubble Sort:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
for (int i = 0; i < n; i++) {
// Track number of elements swapped during a single array traversal
int numberOfSwaps = 0;

for (int j = 0; j < n - 1; j++) {
// Swap adjacent elements if they are in decreasing order
if (a[j] > a[j + 1]) {
swap(a[j], a[j + 1]);
numberOfSwaps++;
}
}

// If no elements were swapped during a traversal, array is sorted
if (numberOfSwaps == 0) {
break;
}
}

Task

Given an array, a, of size n distinct elements, sort the array in ascending order using the Bubble Sort algorithm above. Once sorted, print the following 3 lines:

  1. Array is sorted in numSwaps swaps.
    where numSwaps is the number of swaps that took place.
  2. First Element: firstElement
    where firstElement is the first element in the sorted array.
  3. Last Element: lastElement
    where lastElement is the last element in the sorted array.

Hint: To complete this challenge, you will need to add a variable that keeps a running tally of all swaps that occur during execution.

Input Format

The first line contains an integer, n, denoting the number of elements in array a. The second line contains n space-separated integers describing the respective values of a0, a1, …, an-1.

Constraints

  • 2 <= n <= 600
  • 1 <= ai <= 2 X 106, where 0 <= i < n

Output Format

Print the following three lines of output:

  1. Array is sorted in numSwaps swaps.
    where numSwaps is the number of swaps that took place.
  2. First Element: firstElement
    where firstElement is the first element in the sorted array.
  3. Last Element: lastElement
    where lastElements is the last element in the sorted array.

Sample Input 0

1
2
3
1 2 3

Sample Output 0

1
2
3
Array is sorted in 0 swaps.
First Element: 1
Last Element: 3

Explanation 0

The array is already sorted, so 0 swaps take place and we print the necessary 3 lines of output shown above.

Sample Input 1

1
2
3
3 2 1

Sample Output 1

1
2
3
Array is sorted in 3 swaps.
First Element: 1
Last Element: 3

Explanation 1

The array a = [3, 2, 1] is not sorted, so we perform the following swaps:

  1. [3, 2, 1] -> [2, 3, 1]
  2. [2, 3, 1] -> [2, 1, 3]
  3. [2, 1, 3] -> [1, 2, 3]

At this point the array is sorted and we print the necessary 3 lines of output shown above.




Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function main() {
var n = parseInt(readLine());
a = readLine().split(' ');
a = a.map(Number);
// Write Your Code Here
let numberOfSwaps = 0;

const bubbleSort = (values) => {
let swapped;

do {
swapped = false;

for (let i = 0, { length } = values; i < length; i++) {
if (values[i] > values[i + 1]) {
let tmp = values[i];

values[i] = values[i + 1];
values[i + 1] = tmp;

swapped = true;
numberOfSwaps++;
}
}
} while (swapped);

return values;
};

let result = bubbleSort(a);

console.log(`Array is sorted in ${numberOfSwaps} swaps.\nFirst Element: ${result[0]}\nLast Element: ${result[n - 1]}`);
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×