bubble sort java_Bubble sort (Java)

Given an array of integers, sort the array in ascending order using the Bubble Sort algorithm. Once sorted, print the following three lines:Array is sorted in numSwaps swaps., where is the number of swaps that took place.

First Element: firstElement, where is the first element in the sorted array.

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.

For example, given a worst-case but small array to sort [6,4,1] :

we go through the following steps:

swap a

0 [6,4,1]

1 [4,6,1]

2 [4,1,6]

3 [1,4,6]

It took 3 swaps to sort the array. Output would be

Array is sorted in 3 swaps.

First Element: 1

Last Element: 6

Function Description

Complete the function countSwaps in the editor below. It should print the three lines required, then return.

countSwaps has the following parameter(s):a: an array of integers .

Input Format

The first line contains an integer, the size of the array .

The second line contains space-separated integers .

Output Format

You must print the following three lines of output:Array is sorted in numSwaps swaps., where is the number of swaps that took place.

First Element: firstElement, where is the first element in the sorted array.

Last Element: lastElement, where is the last element in the sorted array.

Sample Input 0

3

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 and we print the necessary three lines of output shown above.

Sample Input 1

3

3 2 1

Sample Output 1

Array is sorted in 3 swaps.

First Element: 1

Last Element: 3

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

import java.io.*;

import java.math.*;

import java.security.*;

import java.text.*;

import java.util.*;

import java.util.concurrent.*;

import java.util.regex.*;

public class Solution {

// Complete the countSwaps function below. static void countSwaps(int[] a) {

int swap=0; int FE=0;int LE=0; int temp;

for (int i = 0; i < a.length -1; i++) {

for (int j = 0; j < a.length-i-1; j++) {

// Swap adjacent elements if they are in decreasing order if (a[j] > a[j + 1]) {

temp = a[j];

a[j] = a[j + 1];

a[j + 1] = temp;

swap=swap+1;

}

}

}

FE=a[0];LE=a[a.length-1];

System.out.printf("Array is sorted in %d swaps.\nFirst Element: %d \nLast Element: %d\n ", swap, FE, LE);

}

private static final Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {

int n = scanner.nextInt();

scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

//'\u0085')、 行分隔符 ('\u2028')或 段落分隔符 ('\u2029)

int[] a = new int[n];

String[] aItems = scanner.nextLine().split(" ");

scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

for (int i = 0; i < n; i++) {

int aItem = Integer.parseInt(aItems[i]);

a[i] = aItem;

}

countSwaps(a);

scanner.close();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值