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

चंद्रयान-2,

Chandrayan-2: चांद के और करीब पहुंचा चंद्रयान-2, जानिए-           पता लगाएगा गुरुवार की रात उपग्रह को पृथ्वी की अगली कक्षा में प्रवेश कराया गया। 29 जुलाई को तीसरी बार चंद्रयान-2 की कक्षा को बदला जाएगा।   बेंगलुरु, प्रेट्र।  Chandrayaan-2  उपग्रह गुरुवार की रात सफलतापूर्वक चंद्रमा की तरफ और एक कदम बढ़ गया। अंतरिक्षयान पर लगे प्रणोदन प्रणाली के जरिए गुरुवार-शुक्रवार की रात एक बजकर आठ मिनट पर दूसरी बार उपग्रह की कक्षा को बदलते हुए उसे सफलतापूर्वक पृथ्वी की अगली कक्षा में प्रवेश कराया गया। भारतीय अंतरिक्ष अनुसंधान संगठन (इसरो) ने बताया कि कक्षा बदलने की यह प्रक्रिया लगभग 15 मिनट तक चली। की ऊंचाई पर जीएसएलवी-मैक 3 रॉकेट से अलग हो गया था और चक्कर लगा रहा था। बुधवार 24 जुलाई को पहली बार उपग्रह को पृथ्वी की अगली कक्षा में भेजा गया था। शुक्रवार को दूसरी बार उसके कक्षा में बदलाव किया गया। छह अगस्त तक अभी और दो बार चंद्रयान-2 की कक्षा (ऑर्बिट) को बदला जाएगा। चंद्रयान-2 पृथ्वी की कक्षा से 14 अगस्त को चंद...
 THE  An avid hiker keeps meticulous records of their hikes. During the last hike that took exactly     steps, for every step it was noted if it was an   uphill ,   , or a   downhill ,     step. Hikes always start and end at sea level, and each step up or down represents a     unit change in altitude. We define the following terms: A  mountain  is a sequence of consecutive steps  above  sea level, starting with a step  up  from sea level and ending with a step  down  to sea level. A  valley  is a sequence of consecutive steps  below  sea level, starting with a step  down  from sea level and ending with a step  up  to sea level. Given the sequence of  up  and  down  steps during a hike, find and print the number of  valleys  walked through. Example   The hiker first enters a valley   units deep. Then they climb...