Skip to main content
added 218 characters in body
Source Link
/**
 * MergeSort.c
 *
 * Ujjwal Arora
 * 
 *
 * Implements merge sort for an array
 */

#include<stdio.h>
#include<conio.h>
void merge(int array[6],int beg,int mid,int end);
void mergesort(int array[6],int beg,int end);
void display(int array[6]);

int main(void)
{
     // Initializes the array
     int array[6]={3,1,2,9,5,4};
     // Initialize the beginning and the end 
     int beg=0, end=5;
     // Implement the Merge Sort 
     mergesort(array,beg,end);
     getch();
}
void mergesort(int array[6],int beg,int end) //Calls Initial merge sort
{
      int mid;
      mid=(beg+end)/2;
      while if(beg<end)
      {
            mid=(beg+end)/2;
            mergesort(array,beg,mid);        //Left part of the array
            mergesort(array,mid+1,end);      //Right part of the array
            merge(array,beg,mid,end);        //merge two sorted arrays
      }
 }
void merge(int array[6],int beg,int mid,int end)
{
     int temp[6]; //Declare a temp array for 
 storing the sorted data
  int k=beg;  //Initialize the temp array
     int i=beg;k=beg;
     //initialize the pointers for two sub arrays     
     int i=beg;
     int j=mid;
 
     while (i<midi<=mid && j<endj<=end)
       {
            if(array[i]<array[j]array[i] < array[j])
              {
                 temp[k]=array[i];
                 i++;
                  k++;    
              }
            else
              {
                 temp[k]=array[j];
                 j++;
                  k++;    
              }
                 k++;              
        }            
    //Clearing any remaining elements in the sub array
     while if(i<midi>mid) 
      {
           temp[k]=array[i];for(int m=j;j<=end;m++)
           i++;{
                  temp[k]= array[m];
                  k++;                      
           }               
    //Clearing any remaining} elements in the sub array   
    whileelse 
      {
           for(j<end)int m=i;m<=mid;m++)
           {
          temp[k]=array[j];        temp[k]= array[m];
          j++;        k++;
          k++; }
      }
    //Reassign the sorted elements to the original array
    for(i=0,k=0;i<end,k<end;i++,k++k=beg;k<end;k++)     
      {
          array[i]=temp[k];array[k]=temp[k];                                      
      }  
    display(array);           //display array
}
void display(int array[6])
{
     for (int i=0;i<5;i++i=0;i<6;i++)
     {
             printf("%d""%d ",array[i]);
     }
     printf("\n");
}
/**
 * MergeSort.c
 *
 * Ujjwal Arora
 * 
 *
 * Implements merge sort for an array
 */

#include<stdio.h>
#include<conio.h>
void merge(int array[6],int beg,int mid,int end);
void mergesort(int array[6],int beg,int end);
void display(int array[6]);

int main(void)
{
     // Initializes the array
     int array[6]={3,1,2,9,5,4};
     // Initialize the beginning and the end 
     int beg=0, end=5;
     // Implement the Merge Sort 
     mergesort(array,beg,end);
     getch();
}
void mergesort(int array[6],int beg,int end) //Calls Initial merge sort
{
      int mid;
      mid=(beg+end)/2;
      while (beg<end)
      {
            mergesort(array,beg,mid);        //Left part of the array
            mergesort(array,mid+1,end);      //Right part of the array
            merge(array,beg,mid,end);        //merge two sorted arrays
      }
 }
void merge(int array[6],int beg,int mid,int end)
{
     int temp[6]; //Declare a temp array for 
      int k=beg;     
     int i=beg; //initialize the pointers for two sub arrays
     int j=mid;
 
     while (i<mid && j<end)
       {
            if(array[i]<array[j])
              {
                 temp[k]=array[i];
                 i++;
                  k++;
              }
            else
              {
                 temp[k]=array[j];
                 j++;
                  k++;
              }              
        }
    //Clearing any remaining elements in the sub array
     while (i<mid) 
      {
           temp[k]=array[i];
           i++;
           k++; 
      }
    //Clearing any remaining elements in the sub array
    while (j<end) 
      {
          temp[k]=array[j];
          j++;
          k++; 
      }
    //Reassign the sorted elements to the original array
    for(i=0,k=0;i<end,k<end;i++,k++)     
      {
          array[i]=temp[k];                                      
      }  
    display(array);           //display array
}
void display(int array[6])
{
     for (int i=0;i<5;i++)
     {
             printf("%d",array[i]);
     }
     printf("\n");
}
/**
 * MergeSort.c
 *
 * Ujjwal Arora
 * 
 *
 * Implements merge sort for an array
 */

#include<stdio.h>
#include<conio.h>
void merge(int array[6],int beg,int mid,int end);
void mergesort(int array[6],int beg,int end);
void display(int array[6]);

int main(void)
{
     // Initializes the array
     int array[6]={3,1,2,9,5,4};
     // Initialize the beginning and the end 
     int beg=0, end=5;
     // Implement the Merge Sort 
     mergesort(array,beg,end);
     getch();
}
void mergesort(int array[6],int beg,int end) //Calls Initial merge sort
{
      int mid;
      
      if(beg<end)
      {
            mid=(beg+end)/2;
            mergesort(array,beg,mid);        //Left part of the array
            mergesort(array,mid+1,end);      //Right part of the array
            merge(array,beg,mid,end);        //merge two sorted arrays
      }
 }
void merge(int array[6],int beg,int mid,int end)
{
     int temp[6]; //Declare a temp array for storing the sorted data
     //Initialize the temp array
     int k=beg;
     //initialize the pointers for two sub arrays     
     int i=beg;
     int j=mid;
 
     while(i<=mid && j<=end)
       {
            if(array[i] < array[j])
              {
                 temp[k]=array[i];
                 i++;                     
              }
            else
              {
                 temp[k]=array[j];
                 j++;                     
              }
                 k++;              
        }            
    //Clearing any remaining elements in the sub array
     if(i>mid) 
      {
           for(int m=j;j<=end;m++)
           {
                  temp[k]= array[m];
                  k++;                      
           }               
      }        
    else 
      {
           for(int m=i;m<=mid;m++)
           {
                  temp[k]= array[m];
                  k++;
           }
      }
    //Reassign the sorted elements to the original array
    for(k=beg;k<end;k++)     
      {
          array[k]=temp[k];                                      
      }  
 display(array);           //display array
}
void display(int array[6])
{
     for(int i=0;i<6;i++)
     {
             printf("%d ",array[i]);
     }
     printf("\n");
}
added 497 characters in body
Source Link
/**
 * MergeSort.c
 *
 * Ujjwal Arora
 * 
 *
 * Implements merge sort for an array
 */

#include<stdio.h>
#include<conio.h>
void merge(int array[6],int beg,int mid,int end);
void mergesort(int array[6],int beg,int end);
void display(int array[6]); 

int main(void)
{
     // Initializes the array
     int array[6]={3,1,2,9,5,4};
     // Initialize the beginning and the end 
     int beg=0, end=5;
     // Implement the Merge Sort 
     mergesort(array,beg,end);
     getch();
}
void mergesort(int array[6],int beg,int end) //Calls Initial merge sort
{
      int mid;
      mid=(beg+end)/2;
      while (beg<end)
      {
                    mergesort(array,beg,mid);        //Left part of the array
                    mergesort(array,mid+1,end);      //Right part of the array
                    merge(array,beg,mid,end);        //merge two sorted arrays
                    }
      }
void merge(int array[6],int beg,int mid,int end)
{
     int temp[6]; //Declare a temp array for 
     int k=0;k=beg;     
     int i=beg; //initialize the pointers for two sub arrays
     int j=mid;
 
     while (i<mid && j<end)
       {
            if(array[i]<array[j])
              {
                 temp[k]=array[i];
                 i++;
                 k++;
              }
            else
              {
                 temp[k]=array[j];
                 j++;
                 k++;
              }              
        }
 while(i<mid)   //Clearing any remaining elements in the sub array
     while (i<mid) 
      {
           temp[k]=array[i];
           i++;
       k++; 
    k++;  
      }
 while(j<end)   //Clearing any remaining elements in the sub array
    while (j<end) 
      {
          temp[k]=array[j];
          j++;
          k++; 
       }
 
  for(i=0,k=0;i<end,k<end;i++,k++)  //Reassign the sorted elements to the original array
    for(i=0,k=0;i<end,k<end;i++,k++)     
      {
          array[i]=temp[k];
                                       
      }  
    display(array);           //display array
}
void display(int array[6])
{
     for (int i=0;i<5;i++)
     {
             printf("%d",array[i]);
             }
     printf("\n");
     }
#include<stdio.h>
#include<conio.h>
void merge(int array[6],int beg,int mid,int end);
void mergesort(int array[6],int beg,int end);
void display(int array[6]);
int main(void)
{
 int array[6]={3,1,2,9,5,4}; 
 int beg=0, end=5;
 mergesort(array,beg,end);
  getch();
}
void mergesort(int array[6],int beg,int end) //Calls Initial merge sort
{
      int mid;
      mid=(beg+end)/2;
      while(beg<end)
      {
                    mergesort(array,beg,mid);        //Left part of the array
                    mergesort(array,mid+1,end);      //Right part of the array
                    merge(array,beg,mid,end);        //merge two sorted arrays
                    }
      }
void merge(int array[6],int beg,int mid,int end)
{
 int temp[6]; //Declare a temp array for 
 int k=0;
 int i=beg; //initialize the pointers for two sub arrays
 int j=mid;
 while(i<mid && j<end)
 {
  if(array[i]<array[j])
  {
   temp[k]=array[i];
   i++;
   k++;
  }
  else
  {
      temp[k]=array[j];
      j++;
      k++;
      }              
 }
 while(i<mid) //Clearing any remaining elements in the sub array
 {
       temp[k]=array[i];
       i++;
       k++; 
         }
 while(j<end) //Clearing any remaining elements in the sub array
 {
       temp[k]=array[j];
       j++;
       k++; 
       }
 
  for(i=0,k=0;i<end,k<end;i++,k++) //Reassign the sorted elements to the original array
 {
  array[i]=temp[k];
                                   
 }  
 display(array);           //display array
}
void display(int array[6])
{
     for(int i=0;i<5;i++)
     {
             printf("%d",array[i]);
             }
     printf("\n");
     }
/**
 * MergeSort.c
 *
 * Ujjwal Arora
 * 
 *
 * Implements merge sort for an array
 */

#include<stdio.h>
#include<conio.h>
void merge(int array[6],int beg,int mid,int end);
void mergesort(int array[6],int beg,int end);
void display(int array[6]); 

int main(void)
{
     // Initializes the array
     int array[6]={3,1,2,9,5,4};
     // Initialize the beginning and the end 
     int beg=0, end=5;
     // Implement the Merge Sort 
     mergesort(array,beg,end);
     getch();
}
void mergesort(int array[6],int beg,int end) //Calls Initial merge sort
{
      int mid;
      mid=(beg+end)/2;
      while (beg<end)
      {
            mergesort(array,beg,mid);        //Left part of the array
            mergesort(array,mid+1,end);      //Right part of the array
            merge(array,beg,mid,end);        //merge two sorted arrays
      }
 }
void merge(int array[6],int beg,int mid,int end)
{
     int temp[6]; //Declare a temp array for 
     int k=beg;     
     int i=beg; //initialize the pointers for two sub arrays
     int j=mid;
 
     while (i<mid && j<end)
       {
            if(array[i]<array[j])
              {
                 temp[k]=array[i];
                 i++;
                 k++;
              }
            else
              {
                 temp[k]=array[j];
                 j++;
                 k++;
              }              
        }
    //Clearing any remaining elements in the sub array
     while (i<mid) 
      {
           temp[k]=array[i];
           i++;
           k++;  
      }
    //Clearing any remaining elements in the sub array
    while (j<end) 
      {
          temp[k]=array[j];
          j++;
          k++; 
      }
    //Reassign the sorted elements to the original array
    for(i=0,k=0;i<end,k<end;i++,k++)     
      {
          array[i]=temp[k];                                      
      }  
    display(array);           //display array
}
void display(int array[6])
{
     for (int i=0;i<5;i++)
     {
             printf("%d",array[i]);
     }
     printf("\n");
}
added 286 characters in body
Source Link
#include<stdio.h>
#include<conio.h>
void merge(int array[6],int beg,int mid,int end);
void mergesort(int array[6],int beg,int end);
void display(int array[6]);
int main(void)
{
 int array[6]={3,1,2,9,5,4}; 
 int beg=0, end=5;
 mergesort(array,beg,end);
  getch();
}
void mergesort(int array[6],int beg,int end) //Calls Initial merge sort
{
      int mid;
      mid=(beg+end)/2;
      while(beg<end)
      {
                    mergesort(array,beg,mid);        //Left part of the array
                    mergesort(array,mid+1,end);      //Right part of the array
                    merge(array,beg,mid,end);        //merge two sorted arrays
                    }
      }
void merge(int array[6],int beg,int mid,int end)
{
 int temp[6]; //Declare a temp array for 
 int k=0;
 int i=beg; //initialize the pointers for two sub arrays
 int j=mid;
 while(i<mid && j<end)
 {
  if(array[i]<array[j])
  {
   temp[k]=array[i];
   i++;
   k++;
  }
  else
  {
      temp[k]=array[j];
      j++;
      k++;
      }              
 }
 while(i<mid) //Clearing any remaining elements in the sub array
 {
       temp[k]=array[i];
       i++;
       k++; 
        }
 while(j<end) //Clearing any remaining elements in the sub array
 {
       temp[k]=array[j];
       j++;
       k++; 
       }
 
 for(i=0,k=0;i<end,k<end;i++,k++) //Reassign the sorted elements to the original array
 {
  array[i]=temp[k];
                                  
 }  
 display(array);           //display array
}
void display(int array[6])
{
     for(int i=0;i<5;i++)
     {
             printf("%d",array[i]);
             }
     printf("\n");
     }

#include<conio.h> void merge(int array[6],int beg,int mid,int end); void mergesort(int array[6],int beg,int end); void display(int array[6]); int main(void) { int array[6]={3,1,2,9,5,4}; int beg=0, end=5; mergesort(array,beg,end); getch(); } void mergesort(int array[6],int beg,int end) //Calls Initial merge sort { int mid; mid=(beg+end)/2; while(beg<end) { mergesort(array,beg,mid); //Left part of the array mergesort(array,mid+1,end); //Right part of the array merge(array,beg,mid,end); //merge two sorted arrays } } void merge(int array[6],int beg,int mid,int end) { int temp[6]; //Declare a temp array for int k=0; int i=beg; //initialize the pointers for two sub arrays int j=mid; while(i<mid && j<end) { if(array[i]<array[j]) { temp[k]=array[i]; i++; k++; } else { temp[k]=array[j]; j++; k++; }
} while(i<mid) //Clearing any remaining elements in the sub array { temp[k]=array[i]; i++; k++; } while(j<end) //Clearing any remaining elements in the sub array { temp[k]=array[j]; j++; k++; }

for(i=0,k=0;i<end,k<end;i++,k++) //Reassign the sorted elements to the original array { array[i]=temp[k];

}
display(array); //display array } void display(int array[6]) { for(int i=0;i<5;i++) { printf("%d",array[i]); } printf("\n"); }

#include<stdio.h>

#include<conio.h> void merge(int array[6],int beg,int mid,int end); void mergesort(int array[6],int beg,int end); void display(int array[6]); int main(void) { int array[6]={3,1,2,9,5,4}; int beg=0, end=5; mergesort(array,beg,end); getch(); } void mergesort(int array[6],int beg,int end) //Calls Initial merge sort { int mid; mid=(beg+end)/2; while(beg<end) { mergesort(array,beg,mid); //Left part of the array mergesort(array,mid+1,end); //Right part of the array merge(array,beg,mid,end); //merge two sorted arrays } } void merge(int array[6],int beg,int mid,int end) { int temp[6]; //Declare a temp array for int k=0; int i=beg; //initialize the pointers for two sub arrays int j=mid; while(i<mid && j<end) { if(array[i]<array[j]) { temp[k]=array[i]; i++; k++; } else { temp[k]=array[j]; j++; k++; }
} while(i<mid) //Clearing any remaining elements in the sub array { temp[k]=array[i]; i++; k++; } while(j<end) //Clearing any remaining elements in the sub array { temp[k]=array[j]; j++; k++; }

for(i=0,k=0;i<end,k<end;i++,k++) //Reassign the sorted elements to the original array { array[i]=temp[k];

}
display(array); //display array } void display(int array[6]) { for(int i=0;i<5;i++) { printf("%d",array[i]); } printf("\n"); }

#include<stdio.h>
#include<conio.h>
void merge(int array[6],int beg,int mid,int end);
void mergesort(int array[6],int beg,int end);
void display(int array[6]);
int main(void)
{
 int array[6]={3,1,2,9,5,4}; 
 int beg=0, end=5;
 mergesort(array,beg,end);
  getch();
}
void mergesort(int array[6],int beg,int end) //Calls Initial merge sort
{
      int mid;
      mid=(beg+end)/2;
      while(beg<end)
      {
                    mergesort(array,beg,mid);        //Left part of the array
                    mergesort(array,mid+1,end);      //Right part of the array
                    merge(array,beg,mid,end);        //merge two sorted arrays
                    }
      }
void merge(int array[6],int beg,int mid,int end)
{
 int temp[6]; //Declare a temp array for 
 int k=0;
 int i=beg; //initialize the pointers for two sub arrays
 int j=mid;
 while(i<mid && j<end)
 {
  if(array[i]<array[j])
  {
   temp[k]=array[i];
   i++;
   k++;
  }
  else
  {
      temp[k]=array[j];
      j++;
      k++;
      }              
 }
 while(i<mid) //Clearing any remaining elements in the sub array
 {
       temp[k]=array[i];
       i++;
       k++; 
        }
 while(j<end) //Clearing any remaining elements in the sub array
 {
       temp[k]=array[j];
       j++;
       k++; 
       }
 
 for(i=0,k=0;i<end,k<end;i++,k++) //Reassign the sorted elements to the original array
 {
  array[i]=temp[k];
                                  
 }  
 display(array);           //display array
}
void display(int array[6])
{
     for(int i=0;i<5;i++)
     {
             printf("%d",array[i]);
             }
     printf("\n");
     }
Source Link
Loading