Skip to main content

 following version of Bubble Sort:

for (int i = 0; i < n; i++) {
    
    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]);
        }
    }
    
}

Given an array of integers, sort the array in ascending order using the Bubble Sort algorithm above. Once sorted, print the following three lines:

  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.

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

Example

swap    a       
0       [6,4,1]
1       [4,6,1]
2       [4,1,6]
3       [1,4,6]

The steps of the bubble sort are shown above. It took  swaps to sort the array. Output is:

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

Function Description

Complete the function countSwaps in the editor below.

countSwaps has the following parameter(s):

  • int a[n]: an array of integers to sort

Prints

Print the three lines required, then return. No return value is expected.

Input Format

The first line contains an integer, , the size of the array .
The second line contains  space-separated integers .

Constraints

Output Format

Sample Input 0

STDIN   Function
-----   --------
3       a[] size n = 3
1 2 3   a = [1, 2, 3]

Sample Output 0

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

Explanation 0
The array is already sorted, so  swaps take place.

Sample Input 1

3
3 2 1

Sample Output 1

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

Explanation 1
The array is not sorted, and its initial values are: . The following  swaps take place:

At this point the array is sorted and the three lines of output are printed to 



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 += currentSwaps
            
    if currentSwaps == 0:
        break
        
print("Array is sorted in " + str(numSwaps) + " swaps.")
print("First Element: " + str(a[0]))
print("Last Element: " + str(a[-1]))




thise problem hackerank...

Comments

Popular posts from this blog

hackerrank

AAlex works at a clothing store. There is a large pile of socks that must be paired by color for sale. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are. For example, there are   socks with colors  . There is one pair of color   and one of color  . There are three odd socks left, one of each color. The number of pairs is  . .AFunction Description Complete the  sockMerchant  function in the editor below. It must return an integer representing the number of matching pairs of socks that are available. asockMerchant has the following parameter(s): n : the number of socks in the pile ar : the colors of each sock .Input Format The first line contains an integer  , the number of socks represented in  . The second line contains   space-separated integers describing the colors   of the socks in the pile. Constraints  where  .Output Fo...

चंद्रयान-2,

Chandrayan-2: चांद के और करीब पहुंचा चंद्रयान-2, जानिए-           पता लगाएगा गुरुवार की रात उपग्रह को पृथ्वी की अगली कक्षा में प्रवेश कराया गया। 29 जुलाई को तीसरी बार चंद्रयान-2 की कक्षा को बदला जाएगा।   बेंगलुरु, प्रेट्र।  Chandrayaan-2  उपग्रह गुरुवार की रात सफलतापूर्वक चंद्रमा की तरफ और एक कदम बढ़ गया। अंतरिक्षयान पर लगे प्रणोदन प्रणाली के जरिए गुरुवार-शुक्रवार की रात एक बजकर आठ मिनट पर दूसरी बार उपग्रह की कक्षा को बदलते हुए उसे सफलतापूर्वक पृथ्वी की अगली कक्षा में प्रवेश कराया गया। भारतीय अंतरिक्ष अनुसंधान संगठन (इसरो) ने बताया कि कक्षा बदलने की यह प्रक्रिया लगभग 15 मिनट तक चली। की ऊंचाई पर जीएसएलवी-मैक 3 रॉकेट से अलग हो गया था और चक्कर लगा रहा था। बुधवार 24 जुलाई को पहली बार उपग्रह को पृथ्वी की अगली कक्षा में भेजा गया था। शुक्रवार को दूसरी बार उसके कक्षा में बदलाव किया गया। छह अगस्त तक अभी और दो बार चंद्रयान-2 की कक्षा (ऑर्बिट) को बदला जाएगा। चंद्रयान-2 पृथ्वी की कक्षा से 14 अगस्त को चंद...