A blog to help students to learn programming in various languages including C, C++, Java, Python and many more.

Subscribe For Free Latest Updates!

We'll not spam mate! We promise.

Showing posts with label sort. Show all posts
Showing posts with label sort. Show all posts

Saturday, 23 January 2016


Program Screenshot
Output

#include <iostream>
using namespace std;
int mergeSort(int a[],int beg,int end);
int merge(int a[],int beg,int mid,int end);
int main()
{
int n,a[100];
cout<<"Enter no. of values: ";
cin>>n;
cout<<"Enter "<<n<<" values: ";
for(int x=0;x<n;x++) {
cin>>a[x];
}
mergeSort(a,0,n-1);
cout<<"Array after Sorting using Merge Sort:\n";
for(int x=0;x<n;x++) {
cout<<a[x]<<"\n";
}
return(0);
}
int mergeSort(int a[],int beg,int end)
{
if(end <= beg) {
return(0);
}
int mid=(beg+end)/2;
mergeSort(a,beg,mid);
mergeSort(a,mid+1,end);
merge(a,beg,mid,end);
}
int merge(int a[],int beg,int mid,int end)
{
int h=beg,i=beg,j=mid+1;
int b[100];
while(h<=mid && j<=end) {
if(a[h]<=a[j]) {
b[i]=a[h];
i++;
h++;
} else {
b[i]=a[j];
i++;
j++;
}
}
if(h>mid) {
for(int k=j;k<=end;k++) {
b[i]=a[k];
i++;
}
} else {
for(int k=h;k<=mid;k++) {
b[i]=a[k];
i++;
}
}
for(int k=beg;k<=end;k++) {
a[k]=b[k];
}
}

Read More >>


Program Screenshot
Output


#include <iostream>

using namespace std;
int insertionSort(int a[],int n);
int main()
{
int n,data,a[100];
cout<<"Enter no. of values: ";
cin>>n;
cout<<"Enter "<<n<<" values: ";
for(int x=0;x<n;x++) {
cin>>a[x];
}
insertionSort(a,n);
cout<<"Array after Sorting using Insertion Sort: \n";
for(int x=0;x<n;x++) {
cout<<a[x]<<"\n";
}
return(0);
}
int insertionSort(int a[],int n)
{
int pivot,temp;
for(int startPos=1;startPos<n;startPos++) {
pivot=startPos;
while(pivot >= 1 && a[pivot] < a[pivot-1]) {
temp=a[pivot];
a[pivot]=a[pivot-1];
a[pivot-1]=temp;
pivot--;
}
}
}

Read More >>

Saturday, 16 January 2016


Program ScreenShot

#include <iostream>

using namespace std;
int quickSort(int a[],int l,int r);
int main()
{
int n,data,a[100];
cout<<"Enter no. of values: ";
cin>>n;
cout<<"Enter "<<n<<" values : ";
for(int x=0;x<n;x++) {
cin>>a[x];
}
quickSort(a,0,n);
cout<<"Array after Sorting using Quick Sort: \n";
for(int x=0;x<n;x++) {
cout<<a[x]<<"\n";
}
return(0);
}
int quickSort(int a[],int l,int r)
{
int temp;
if(r-l<=1) {
return(0);
}
int y=l+1;
for(int g=l+1;g<r;g++) {
if(a[g]<=a[l]) {
temp=a[y];
a[y]=a[g];
a[g]=temp;
y++;
}
}
temp=a[y-1];
a[y-1]=a[l];
a[l]=temp;
quickSort(a,l,y-1);
quickSort(a,y,r);
}
Read More >>


Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. 

The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted. The algorithm, which is a comparison sort, is named for the way smaller elements "bubble" to the top of the list. Although the algorithm is simple, it is too slow and impractical for most problems even when compared to insertion sort. It can be practical if the input is usually in sorted order but may occasionally have some out-of-order elements nearly in position.

Performance:->

  • Best case: O(n)
  • Average case: О(n2)
  • Worst case: О(n2)

Information source: wikipedia.org


Program:->

Program Output
Output
#include <iostream>
using namespace std;
int bubble(int *ptr,int n);
int main()
{
int n,data,a[100];
int *ptr;
ptr=a;
cout<<"Enter no. of values: ";
cin>>n;
cout<<"Enter "<<n<<" values: ";
for(int x=0;x<n;x++) {
cin>>*(ptr+x);
}
bubble(ptr,n);
cout<<"Array after Sorting using BUBBLE Sort: \n";
for(int x=0;x<n;x++) {
cout<<*(ptr+x)<<"\n";
}
return(0);
}
int bubble(int *ptr,int n)
{
int x,temp;
for(int k=0;k<n;k++) {
x=0;
while(x<n-k-1) {
if(*(ptr+x)>*(ptr+x+1)) {
temp=*(ptr+x+1);
*(ptr+x+1)=*(ptr+x);
*(ptr+x)=temp;
}
x=x+1;
}
}
}

Read More >>


Copyright © 2016 - ProgrmIt - All Rights Reserved
(Articles Cannot Be Reproduced Without Author Permission.)
Design By : | Powered By: Blogger