Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

bubble sort

#include<iostream>
void swap(int &a, int &b)
{
  int temp=a;
  a=b;
  b=temp;
}
void bubblesort(int arr[], int n)
{
  for(int i=0;i<n-1;i++)// why upto n-1?
  {
    for(int j=0;j<n-1;j++)
    {
      if(arr[j]>arr[j+1])
      {
        swap(arr[j],arr[j+1]);
      }
    }
  }
}
int main()
{
  int arr[5]={7, 8, 1, -4, 0};
  bubblesort(arr, 5);
  for(int i=0;i<5;i++)
  {
    std::cout<<arr[i]<<std::endl;
  }
}
Source by github.com #
 
PREVIOUS NEXT
Tagged: #bubble #sort
ADD COMMENT
Topic
Name
4+5 =