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 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
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cout<<"Enter number of element you want to store: ";
cin>>n;
int arr[n],i,j;
cout<<"Enter array values:\n";
//taking the array value
//from user
for(i=0;i<n;i++)
{
cin>>arr[i];
}
//Now we will sort the array
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
//checking if previous value is
//grater than next one or not
if(arr[j]>arr[j+1])
{
//temp will temporarly store
//the value of arr[j]
//then we will swap the values
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
cout<<"After Bubble sort the array is:\n";
for(i=0;i<n;i++)
cout<<arr[i]<<" ";
return 0;
}
Algorithm of bubble sort
begin BubbleSort(list)
for all elements of list
if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for
return list
end BubbleSort
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