Sort by

recency

|

628 Discussions

|

  • + 0 comments

    import java.io.; import java.util.; import java.util.stream.*;

    public class Solution { public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(bufferedReader.readLine().trim());
        List<Integer> a = Stream.of(bufferedReader.readLine().trim().split("\\s+"))
                                .map(Integer::parseInt)
                                .collect(Collectors.toList());
    
        int totalSwaps = 0;
    
        for (int i = n - 1; i > 0; i--) {
            int swapsInPass = 0;
            for (int j = 0; j < i; j++) {
                if (a.get(j) > a.get(j + 1)) {
                    Collections.swap(a, j, j + 1);
                    swapsInPass++;
                    totalSwaps++;
                }
            }
            if (swapsInPass == 0) break; // Stop early if no swaps in this pass
        }
    
        System.out.println("Array is sorted in " + totalSwaps + " swaps.");
        System.out.println("First Element: " + a.get(0));
        System.out.println("Last Element: " + a.get(n - 1));
    
        bufferedReader.close();
    }
    

    }

  • + 0 comments

    HOW CAN I RECOVER A STOLEN BTC/USDT/ETH? HIRE FIXER WALLET RETRIEVAL Never took much time for me to be able to recover a stolen bitcoin worth of $124k. Reaching out to FIXER WALLET RETRIEVAL, one of the trusted and reputable hackers out here can save you the energy of becoming a stranded victim of crypto fraud. Here is how to contact these hackers; Email fixerwalletretrieval@fixer.co.site

  • + 0 comments
    count=0
        for i in range(n):
            
            for j in range(n-1):
                if a[j]>a[j+1]:
                    a[j],a[j+1]=a[j+1],a[j]
                    count+=1
            if count==0:
                break
        print('Array is sorted in',count,'swaps.')
        print('First Element:', a[0])
        print('Last Element:', a[-1])
    
  • + 0 comments

    Python3 solution *Check on the indentation python if name == 'main': n = int(input().strip())

    a = list(map(int, input().rstrip().split()))
    swap = 0
    #Traverse through all the elements in the array
    for i in range(n):
    
        for j in range(n-1):
            #Traverse the array from 0 to n-1
            #Swap the element if the element is found greater than the next element
            if a[j] > a[j+1]:
                swap += 1
                a[j], a[j+1] = a[j+1], a[j]
    
    print(f'Array is sorted in {swap} swaps.')
    print(f'First Element: {a[0]}')
    print(f'Last Element: {a[-1]}')
    
  • + 0 comments

    JavaScript

    function bubbleSort(array: number[]): void {
        let endPosition: number = array.length - 1;
        let numberOfSwaps: number = 0;
        let swapPosition: number;
    
        while (endPosition > 0) {
            swapPosition = 0;
    
            for (let i = 0; i < array.length - 1; i++) {
                if (array[i] > array[i + 1]) {
                    [array[i], array[i + 1]] = [array[i + 1], array[i]];
                    swapPosition = i;
                    numberOfSwaps++;
                }
            }
            endPosition = swapPosition;
            
        } 
        console.log(`Array is sorted in ${numberOfSwaps} swaps.`);
        console.log(`First Element: ${array[0]}`);
        console.log(`Last Element: ${array[array.length - 1]}`);
    }
    
    function main() {
        const n: number = parseInt(readLine().trim(), 10);
    
        const a: number[] = readLine().replace(/\s+$/g, '').split(' ').map(aTemp => parseInt(aTemp, 10));
    
        bubbleSort(a);
    }