Showing posts with label sorting. Show all posts
Showing posts with label sorting. Show all posts

C program to Find the largest of the numbers entered in an array, using the technique of array sorting

Leave a Comment
/* In the given program written in C, we have used the technique of array sorting instead of sequential search for finding the greatest of the numbers present in the array. The array is first sorted in the descending order, thus the greatest number in the list will be the 1st element of the array i.e., of whose the index number is '0', you can also choose to sort the array in ascending order then the largest number be in the end element of the array*/


#include<stdio.h>
#include<conio.h>
int main()
{
int num[100], a, b, help, n;
printf("how many numbers you want\n");
scanf("%d",&n);
for(a=0;a<n;a++)
{
printf("enter element %d\n",a+1);
scanf("%d",&num[a]);
}
for(a=0;a<n;a++)
{
for(b=a+1;b<n;b++)
{
if (num[b]>num[a])
{
help=num[b];
num[b]=num[a];
num[a]=help;
}
}
}
printf("\nthe largest number is %d",num[0] );
getch();
}
Screenshot:
C program to Find the largest of the numbers entered in an array, using the technique of array sorting
 
Read More...

C++ Program Code is for implementing the insertion sort algorithm on an array, utilizing template class concept

Leave a Comment
/* The following C++ Program Code is for implementing the insertion sort algorithm on an array. To attain the compatibility for various data types, template class concept is used. The user is asked about the number of element he want to put in the array. Then he also enters the elements & the insertion sort algorithm is applied, & thus result is displayed */

#include<iostream>
#include<stdlib.h>
using namespace std;

template<class T>
void isort(T a[],int n)
{
     int i;
     for(i=1;i<n;i++)
     {
             T t=a[i];
             int j;
             for(j=i-1;j>=0&&t<a[j];j--)
             {
                                           a[j+1]=a[j];
             }
             a[j+1]=t;
     }
}


void main()
{
    int a[100],i,n;
    cout<<"Enter number of elements : ";
    cin>>n;
    cout<<"Enter elements (Use Spacebar as Separator)\n";
    for(i=0;i<n;i++)
    {
                    cin>>a[i];
    }
    isort(a,n);
    cout<<"After sorting the elements are\n";
    for(i=0;i<n;i++)
    {
                    cout<<a[i]<<"\n";
    }
    system("pause");
}


Screenshot:
C++ Program Code is for implementing the insertion sort algorithm on an array, utilizing template class concept

Read More...

C++ Program Code for Selection Sort (Ascending Order) of array elements, with template class

7 comments
/*The following C++ Program Code for Selection sorting algorithm implementation which gives a sorted array arranged in ascending order. The template class is used to have flexibility in the type of data, whether integer, float, or character it will work for all. In order to sort the array in descending order, just change the condition  if(a[j]<a[i]) to if(a[j]>a[i])*/

#include<iostream>
#include<conio.h>

using namespace std;


template <class T>
void s_sort(T a[],int n)
{
     int i,j,t;
     for(i=0;i<n;i++)
     {
                     for(j=i+1;j<n;j++)
                     {
                                       if(a[j]<a[i]) //
for descending order use if(a[j]>a[i])
                                       {
                                                    t=a[i];
                                                    a[i]=a[j];
                                                    a[j]=t;
                                       }
                     }
     }
}


int main()
{
    int a[100],i,n;
    cout<<"Enter The number of Element:\n";
    cin>>n;
    cout<<"\nEnter Elements:\n";
    for(i=0;i<n;i++)
    {

                      cout<<"\nEnter:";
                    cin>>a[i];
    }
    s_sort(a,n);
    cout<<"\nAfter Sorting\n";
    for(i=0;i<n;i++)
    {
                    cout<<a[i]<<"\t";
    }
    getch();
    return 0;
}


Screenshot:
C++ Program Code for Selection Sort (Ascending Order) of array elements, with template class
Read More...

C program for merge sorting, using linked list: Creating two linked lists & merge them using Merge sort.

Leave a Comment
#include<stdio.h>                                           //for standard input/output functions
#include<conio.h>                                          //for console input/output functions

struct node
{
int info;
struct node *next;
}*front = NULL,*front1=NULL,*front2=NULL;     
//creating 3 node's pointer variable
 //with initial value NULL, as empty list
 

void insert()                                                             //Function to insert the list item,  
{
struct node *t,*q;
int item;
t = (struct node *)malloc(sizeof(struct node));  
//To reserve the memory space to node's element
 printf("Input the item value to be added in the queue : ");
scanf("%d",&item);                                           
//user inputs the integer data
 t->info = item;
if( front == NULL )                                             
//If the list is still empty
 {
front=t;
front->next=NULL;
}
else                                                                        
//If the list is not empty 
{
q = front;
while( q->next!= NULL )
q=q->next;
t->next = q->next;
q->next = t;
}
}

void insert1()                     
//Function to insert the list item,
{
struct node *t,*q;
int item;
t = (struct node *)malloc(sizeof(struct node));
//To reserve the memory space to node's element
printf("Input the item value to be added in the queue : ");
scanf("%d",&item);
                                         //user inputs the integer data
t->info = item;
if( front == NULL )                                          
//if the list is empty
 {
front=t;
front->next=NULL;
}
else                                                                    
//if the list is not empty 
{
q = front;
while( q->next!= NULL )
q=q->next;
t->next = q->next;
q->next = t;
}
}

void m1_sort(struct node *t)         
//for internal use in function m_sort()
 
struct node *tt;
tt=front2;
while(tt!=NULL)
tt=tt->next;
tt->next=t;
}


void m_sort()                                   //
for sorting the list & merging
 {
struct node *t1,*t,*temp,*tt;
t=front;
t1=front1;
while(t!=NULL||t1!=NULL)        
//for traversing the list till the occurrence of NULL element  
{
if((t->info)<(t1->info))
{
temp=(struct node*)malloc(sizeof(struct node));
temp->info=t->info;
temp->next=NULL;
if(front2==NULL)
front2=temp;
else
{
tt=front2;
while(tt!=NULL)
tt=tt->next;
tt->next=temp;
}
}
else
{
temp=(struct node*)malloc(sizeof(struct node));
temp->info=t1->info;
temp->next=NULL;
if(front2==NULL)
front2=temp;
else
{
tt=front2;
while(tt!=NULL)
tt=tt->next;
tt->next=temp;
}
}
t=t->next;
t1=t1->next;
}
if(t1!=NULL)
m1_sort(t1);
else if(t!=NULL)
m1_sort(t);
}


void display()                         
//to display the list element 
{
struct node *ptr;
ptr = front2;
if(front == NULL)
printf("Queue is empty\n");
else

printf("Queue is :\n");
printf("Priority Item\n");
while(ptr != NULL)
{
printf("%5d\n",ptr->info);
ptr = ptr->next;
}
}
}


int main()
{
int ch;
while(ch!=4)
{
printf("1.Insert in 1st queue\n");
printf("2.Insert in 2nd queue\n");
printf("3.Display\n");
printf("4. Sort!!\n");
printf("5.Quit\n");
printf("Enter your choice : ");
scanf("%d", &ch);                      
//asking the user choice of the list operation
switch(ch)
{
case 1:
insert();
break;
case 2:
insert1();
break;
case 3:
display();
break;
case 4:
m_sort();
break;
case 5:
exit(1);
default :
printf("Wrong choice\n");
}
}

getche();                                     //for exit the program after a keystroke
return 0;
}
Read More...