Answers for "bubble sort example"

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
3

bubble sort c

#include <bits/stdc++.h> 
using namespace std; 
  
void swap(int *xp, int *yp)  
{  
    int temp = *xp;  
    *xp = *yp;  
    *yp = temp;  
}  
  
// A function to implement bubble sort  
void bubbleSort(int arr[], int n)  
{  
    int i, j;  
    for (i = 0; i < n-1; i++)      
      
    // Last i elements are already in place  
    for (j = 0; j < n-i-1; j++)  
        if (arr[j] > arr[j+1])  
            swap(&arr[j], &arr[j+1]);  
}  
  
/* Function to print an array */
void printArray(int arr[], int size)  
{  
    int i;  
    for (i = 0; i < size; i++)  
        cout << arr[i] << " ";  
    cout << endl;  
}  
  
// Driver code  
int main()  
{  
    int arr[] = {64, 34, 25, 12, 22, 11, 90};  
    int n = sizeof(arr)/sizeof(arr[0]);  
    bubbleSort(arr, n);  
    cout<<"Sorted array: \n";  
    printArray(arr, n);  
    return 0;  
}
Posted by: Guest on July-02-2020
1

Bubble Sort

#Bubble Sort
nums = [9, 4, 5, 1, 3, 7, 6]
for i in range(len(nums)):
    for j in range(1, len(nums)):
        if nums[j] < nums[j - 1]:
            nums[j], nums[j - 1] = nums[j - 1], nums[j]
Posted by: Guest on August-15-2021
3

bubble sort code

func Sort(arr []int) []int {
	for i := 0; i < len(arr)-1; i++ {
		for j := 0; j < len(arr)-i-1; j++ {
			if arr[j] > arr[j+1] {
				temp := arr[j]
				arr[j] = arr[j+1]
				arr[j+1] = temp
			}
		}
	}
	return arr
}
Posted by: Guest on August-16-2020
0

bubble sort algorithm

#include <bits/stdc++.h>

using namespace std;

void bubbleSort(int arr[], int n){
    int temp,i,j;
    for(i=0;i<n;i++){
        for(j=i+1;j<n;j++){
            if(arr[i] > arr[j]){
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }
}

int main(){
    int arr[] = {1,7,33,9,444,2,6,33,69,77,22,9,3,11,5,2,77,3};
    int n = sizeof(arr) / sizeof(arr[0]);

    bubbleSort(arr, n);

    for(int i=0;i<n;i++){
        cout << arr[i] << " ";
    }

    return 0;

}
Posted by: Guest on January-16-2021
0

bubble sort

#include<iostream>  
using namespace std;
int main ()
{
   int i, j,temp;
   int a[5] = {10,2,0,43,12};
   cout <<"Input list ...\n";
   for(i = 0; i<5; i++) {
   cout <<a[i]<<"\t";
}
cout<<endl;
for(i = 0; i<5; i++) {
   for(j = i+1; j<5; j++)
   {
      if(a[j] < a[i]) {
         temp = a[i];
         a[i] = a[j];
         a[j] = temp;
      }
   }
}
cout <<"Sorted Element List ...\n";
for(i = 0; i<5; i++) {
   cout <<a[i]<<"\t";
   }
return 0;
}
Posted by: Guest on October-11-2021

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language