shorting

Sorting theory

sorting program 1 c language

#include<stdio.h>
#include<conio.h>
void printArray(int* A, int n){
for(int i=0;i<n;i++)
{
printf("%d  ", A[i]);
}
printf("\n");
}
void bubblesort(int *A,int n){
int temp;
for(int i=0;i< n;i++)// for number of pass
{
for(int j=0;j<n-1-i;j++)// For compariso in each pass
{
if(A[j]>A[j+1]){
temp=A[j];
A[j]= A[j+1];
A[j+1]= temp ;
}
}
}
}
int main(){
clrscr();
int A[] = {12,54,65,7,23,9} ;
int n=6;
printArray(A,n); // printing the array before sorting
bubblesort(A,n); // Function to sort the array
printArray(A,n); // Printing the array before sorting
getch();
}


program :-1

// Java Program to sort an elements
// by bringing Arrays into play
 

class demo {
 
   
    public static void main(String[] args)
    {
 
       
        int arr[] = { 4, 3, 2, 1 };
 
       
        for (int i = 0; i < arr.length; i++) {
 
           
            for (int j = i + 1; j < arr.length; j++) {
 
               
                int temp = 0;
                if (arr[j] < arr[i]) {
 
                   
                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
 
            System.out.print(arr[i] + " ");
        }
    }
}

// Output

// C:\java>javac Test.java

// C:\java>java demo
// 1 2 3 4
// C:\java>



// Java Program to sort an elements
// by bringing Arrays into play
 

class demo {
 
   
   static void bubblesort(int[] arr)
    {
        int n= arr.length;
        int temp=0;
        for(int i=0;i<n;i++){
            for(int j=1;j<(n-i);j++)
                if(arr[j-1]>arr[j]){
                    temp=arr[j-1];
                    arr[j-1]=arr[j];
                    arr[j]=temp;
                }
        }
    }
}
public static void main(String[] args){
    int arr[] ={3,60,35,2,45,320,5};
    System.out.println("array Befoe Bubble sort");
    for(int i=0;i<arr.lenth;i++){
        System.out.print(arr[i]+"");
    }
    System.out.println();
    bubblesort(arr);
    System.out.println;i++){
        System.out.print(arr[i]+"");
    }
}
}
 
       
       
// Output

// C:\java>javac Test.java

// C:\java>java demo
// 1 2 3 4
// C:\java>


#include<stdio.h>
#include<conio.h>
void printArray(int* A, int n){
for(int i=0;i<n;i++)
{
printf("%d  ", A[i]);
}
printf("\n");
}
void bubblesort(int *A,int n){
int temp;
for(int i=0;i< n;i++)// for number of pass
{
for(int j=0;j<n-1-i;j++)// For compariso in each pass
{
if(A[j]>A[j+1]){
temp=A[j];
A[j]= A[j+1];
A[j+1]= temp ;
}
}
}
}
int main(){
clrscr();
int A[] = {12,54,65,7,23,9} ;
int n=6;
printArray(A,n); // printing the array before sorting
bubblesort(A,n); // Function to sort the array
printArray(A,n); // Printing the array before sorting
getch();
}



Bubble sort Program:-51

 class BubbleSort
 {
     public static void main(String args[]){
         int[] a={36,19,29,12,5};
         int temp;
         for(int i=0;i<a.length;i++)
         {
             for(int j=0;j<a.length-1;j++)
             {
                 if(a[j]>a[j+1])
                 {
                     tempa[j];
                     a[j]=a[j+1];
                     a[j+1]=temp;
                 }
             }
         }
         for(int i=0;i<a.length;i++)
         {
             System.out.println(a[i]+" ");
         }
     }

 }
//Output
// C:\VSjava>javac Testing.java

// C:\VSjava>java BubbleSort
// 5
// 12
// 19
// 29
// 36

// C:\VSjava>

Program:-52
 class BubbleSort
 {
     public static void main(String args[]){
         int[] a={36,19,29,12,5};
         int temp;
         for(int i=0;i<a.length;i++)
         { 
             int flag=0;
             for(int j=0;j<a.length-1-i;j++)
             {
                 if(a[j]>a[j+1])
                 {
                     tempa[j];
                     a[j]=a[j+1];
                     a[j+1]=temp;
                     flag=1;
                 }
             }
             if(flag==0){
                 break;
             }
         }
         for(int i=0;i<a.length;i++)
         {
             System.out.println(a[i]+" ");
         }
     }

 }
//Output
// C:\VSjava>javac Testing.java

// C:\VSjava>java BubbleSort
// 5
// 12
// 19
// 29
// 36

// C:\VSjava>
Program:-53
 class BubbleSort
 {
     public static void main(String args[]){
         String[] a={" deepak ","amit ","deepash","vironika ""rahul"};
         String temp;
         for(int i=0;i<a.length;i++)
         { 
             int flag=0;
             for(int j=0;j<a.length-1-i;j++)
             {
                 if(a[j].compareTo(a[j+1])>0)
                 {
                     tempa[j];
                     a[j]=a[j+1];
                     a[j+1]=temp;
                     flag=1;
                 }
             }
             if(flag==0){
                 break;
             }
         }
         for(int i=0;i<a.length;i++)
         {
             System.out.println(a[i]+" ");
         }
     }
 }
//Output
// C:\VSjava>javac Testing.java
// C:\VSjava>java BubbleSort
//  deepak
// amit
// deepash


// rahul
// vironika

Insertion sorting


 
#include<stdio.h>
void printArray(int* A,int n){
    for(int i=0;i<n;i++)
    {
        printf("%d",A[i]);
    }
    printf("\n");
      }

int main(){
int A[] = {12  ,54  ,65 ,7 ,23 ,9 };
int n=6;
printArray(A,n);
 //void insertionsort(A,n);

   
return 0;
}

output

PS C:\java\Datastructure> cd "c:\java\Datastructure\" ; if ($?) { gcc Insertion_sort.c -o Insertion_sort } ; if ($?) { .\Insertion_sort } 1254657239

Program :-3


program :-4

program :-5

program :-6

program :-7

program :-8

program :-9

program :-10




program :-11



                                                                                                                                                                               
                                                                            

Comments