Answers for "bubble sort example step by step in java"

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
0

explain modifide bubble sort code in java

void bubble(int a[], int n){
	for(int i=0; i<n; i++){
		int swaps=0;
		for(int j=0; j<n-i-1; j++){
			if(a[j]>a[j+1]){
				int t=a[j];
				a[j]=a[j+1];
				a[j+1]=t;
				swaps++;
			}
		}
		if(swaps==0)
			break;
	}
}
Posted by: Guest on August-12-2020

Code answers related to "bubble sort example step by step in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language