Answers for "bubble sort analysis"

4

bubble sort in java

public static void bubbleSort(int arr[])
{
	for (int i = 0; i < arr.length; i++) //number of passes
    {
		//keeps track of positions per pass      
    	for (int j = 0; j < (arr.length - 1 - i); j++) //Think you can add a -i to remove uneeded comparisons 
        {
          	//if left value is great than right value 
        	if (arr[j] > arr[j + 1])
            {
              	//swap values
            	int temp = arr[j];
              	arr[j] = arr[j + 1];
              	arr[j + 1] = temp; 
            }
        }
    }
}
Posted by: Guest on April-15-2020
1

bubble sort

/*bubble sort;timecomplexity=O(n){best case}
               time complexity=O(n^2){worst case}
               space complexity=O(n);auxiliary space commplexity=O(1)
*/
#include <iostream>

using namespace std;
void swap(int*,int*);
void bubble_sort(int arr[],int n)
{
    for(int i=0;i<n-1;i++)
    {
        for(int j=0;j<n-1-i;j++)
        {
            if(arr[j]>arr[j+1])
            {
                swap(&arr[j],&arr[j+1]);
            }
        }
    }
}
void display(int arr[],int n)
{
    for(int i=0;i<n;i++)
    {
        cout<<arr[i]<<" ";
    }
    cout<<endl;
}

int main()
{
    int n;
    cout<<"enter the size of the array:"<<endl;
    cin>>n;
    int array_of_numbers[n];
    cout<<"enter the elements of the array"<<endl;
    for(int i=0;i<n;i++)
    {
        cin>>array_of_numbers[i];
    }
    cout<<"array as it was entered:"<<endl;
    display(array_of_numbers,n);
    bubble_sort(array_of_numbers,n);
    cout<<"array after bubble sort:"<<endl;
    display(array_of_numbers,n);
    return 0;
}
void swap(int *a,int *b)
{
    int temp=*a;
    *a=*b;
    *b=temp;
}
Posted by: Guest on May-25-2021
0

Bubble Sort

import static java.lang.Integer.parseInt;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.StringTokenizer;

public class Day20_Sorting {

	static int MB = 1 << 20;
	static BufferedReader BR = new BufferedReader( new InputStreamReader(System.in, StandardCharsets.US_ASCII), 20 * MB);
	
	static StringTokenizer st;
	static String lastLine;
	
	static void newLine() throws IOException {
		lastLine = BR.readLine();
		st = new StringTokenizer(lastLine);
	}
	
	public static void main(String[] args) throws IOException {
		newLine();
		int N = parseInt(st.nextToken());
		
		newLine();
		int[] A = new int[N];
		for (int i = 0; i < N; i++) {
			A[i] = parseInt(st.nextToken());
		}

		int numberOfSwapps = bubbleSort(N, A);
		int firstElement = A[0];
		int lastElement = A[N-1];
		print(numberOfSwapps, firstElement, lastElement);
	}

	private static void print(int numberOfSwapps, int firstElement, int lastElement) {
		StringBuilder sb = new StringBuilder();
		
		sb.append("Array is sorted in ").append(numberOfSwapps).append(" swaps.\n");
		sb.append("First Element: ").append(firstElement).append('\n');
		sb.append("Last Element: ").append(lastElement).append('\n');
		
		System.out.print(sb);
	}

	private static int bubbleSort(int N, int[] A) {
		int cnt = 0;
		
		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 , j + 1);
		            numberOfSwaps++;
		        }
		    }
		    cnt += numberOfSwaps;
		    
		    // If no elements were swapped during a traversal, array is sorted
		    if (numberOfSwaps == 0) {
		        break;
		    }
		}
		
		return cnt;
	}

	private static void swap(int[] a, int i, int j) {
		int tmp = a[i];
		a[i] = a[j];
		a[j] = tmp;
	}

}
Posted by: Guest on April-26-2021
0

c++ buble sort

Psuedo code:

Procedure bubble_sort (array , N)
               array – list of items to be sorted
               N – size of array
begin
               swapped = false
               repeat
                             for I = 1 to N-1 
                                              if array[i-1] > array[i] then
                                                           swap array[i-1] and array[i]
                                                           swapped = true
                                              end if
                              end for
                until not swapped
end procedure
Posted by: Guest on November-30-2020

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language