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]
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]
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
}
Bubble sort
class Sort
{
static void bubbleSort(int arr[], int n)
{
if (n == 1) //passes are done
{
return;
}
for (int i=0; i<n-1; i++) //iteration through unsorted elements
{
if (arr[i] > arr[i+1]) //check if the elements are in order
{ //if not, swap them
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
bubbleSort(arr, n-1); //one pass done, proceed to the next
}
void display(int arr[]) //display the array
{
for (int i=0; i<arr.length; ++i)
{
System.out.print(arr[i]+" ");
}
}
public static void main(String[] args)
{
Sort ob = new Sort();
int arr[] = {6, 4, 5, 12, 2, 11, 9};
bubbleSort(arr, arr.length);
ob.display(arr);
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us