In this article, we will discuss the implementation of Odd-Even Sort
Odd-Even Sort Program in C
Solution In C
Code:
#include<stdio.h>
int main()
{
int size;
printf("Enter the size of the array:");
scanf("%d",&size);
int arr[size],i;
printf("Enter the element of the array:");
for(i=0;i<size;i++)
scanf("%d",&arr[i]);
printf("Before Sorting Array Element are: ");
for(i=0;i<size;i++)
printf("%d ",arr[i]);
int out,in,temp;
for(out=0;out<size;out++){
//Even
for(in=0;in<size-1;in=in+2)
{
if(in!=size-1)
if(arr[in]>arr[in+1])
{
temp=arr[in];
arr[in]=arr[in+1];
arr[in+1]=temp;
}}
//odd
for(in=1;in<size-1;in=in+2)
{
if(in!=size-1)
if(arr[in]>arr[in+1])
{
temp=arr[in];
arr[in]=arr[in+1];
arr[in+1]=temp;
}}}
printf("\nAfter Sorting Array Element are: ");
for(i=0;i<size;i++)
printf("%d ",arr[i]);
}
int main()
{
int size;
printf("Enter the size of the array:");
scanf("%d",&size);
int arr[size],i;
printf("Enter the element of the array:");
for(i=0;i<size;i++)
scanf("%d",&arr[i]);
printf("Before Sorting Array Element are: ");
for(i=0;i<size;i++)
printf("%d ",arr[i]);
int out,in,temp;
for(out=0;out<size;out++){
//Even
for(in=0;in<size-1;in=in+2)
{
if(in!=size-1)
if(arr[in]>arr[in+1])
{
temp=arr[in];
arr[in]=arr[in+1];
arr[in+1]=temp;
}}
//odd
for(in=1;in<size-1;in=in+2)
{
if(in!=size-1)
if(arr[in]>arr[in+1])
{
temp=arr[in];
arr[in]=arr[in+1];
arr[in+1]=temp;
}}}
printf("\nAfter Sorting Array Element are: ");
for(i=0;i<size;i++)
printf("%d ",arr[i]);
}
Input/Output:
Enter the size of the array:5
Enter the element of the array:
4
3
7
8
9
Before Sorting Array Elements are: 4 3 7 8 9
After Sorting Array Elements are: 3 4 7 8 9
7
8
9
Before Sorting Array Elements are: 4 3 7 8 9
After Sorting Array Elements are: 3 4 7 8 9
Odd-Even sort Program in C plus plus
Solution In C++
Code:
#include<iostream>
using namespace std;
int main()
{
int size;
cout<<"Enter the size of the array:";
cin>>size;
int arr[size],i;
cout<<"Enter the element of the array:";
for(i=0;i<size;i++)
cin>>arr[i];
cout<<"Before Sorting Array Element are: ";
for(i=0;i<size;i++)
cout<<arr[i]<<" ";
int out,in,temp;
for(out=0;out<size;out++){
//Even
for(in=0;in<size-1;in=in+2)
{
if(in!=size-1)
if(arr[in]>arr[in+1])
{
temp=arr[in];
arr[in]=arr[in+1];
arr[in+1]=temp;
}}
//odd
for(in=1;in<size-1;in=in+2)
{
if(in!=size-1)
if(arr[in]>arr[in+1])
{
temp=arr[in];
arr[in]=arr[in+1];
arr[in+1]=temp;
}}}
cout<<"\nAfter Sorting Array Element are: ";
for(i=0;i<size;i++)
cout<<arr[i]<<" ";
}
Input/Output:using namespace std;
int main()
{
int size;
cout<<"Enter the size of the array:";
cin>>size;
int arr[size],i;
cout<<"Enter the element of the array:";
for(i=0;i<size;i++)
cin>>arr[i];
cout<<"Before Sorting Array Element are: ";
for(i=0;i<size;i++)
cout<<arr[i]<<" ";
int out,in,temp;
for(out=0;out<size;out++){
//Even
for(in=0;in<size-1;in=in+2)
{
if(in!=size-1)
if(arr[in]>arr[in+1])
{
temp=arr[in];
arr[in]=arr[in+1];
arr[in+1]=temp;
}}
//odd
for(in=1;in<size-1;in=in+2)
{
if(in!=size-1)
if(arr[in]>arr[in+1])
{
temp=arr[in];
arr[in]=arr[in+1];
arr[in+1]=temp;
}}}
cout<<"\nAfter Sorting Array Element are: ";
for(i=0;i<size;i++)
cout<<arr[i]<<" ";
}
Enter the size of the array:4
Enter the element of the array:
7
9
3
5
Before Sorting Array Elements are: 7 9 3 5
After Sorting Array Elements are: 5 3 7 9
Enter the element of the array:
7
9
3
5
Before Sorting Array Elements are: 7 9 3 5
After Sorting Array Elements are: 5 3 7 9
Odd-Even Sort Program in Java
Code:
import java.util.Scanner;
public class Odd_Even_Sort {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int size;
System.out.print("Enter the size of the array:");
size=cs.nextInt();
int arr[]=new int[size],i;
System.out.print("Enter the element of the array:");
for(i=0;i<size;i++)
arr[i]=cs.nextInt();
System.out.print("Before Sorting Array Element are: ");
for(i=0;i<size;i++)
System.out.print(arr[i]+" ");
int out,in,temp;
for(out=0;out<size;out++){
//Even
for(in=0;in<size-1;in=in+2)
{
if(in!=size-1)
if(arr[in]>arr[in+1])
{
temp=arr[in];
arr[in]=arr[in+1];
arr[in+1]=temp;
}}
//odd
for(in=1;in<size-1;in=in+2)
{
if(in!=size-1)
if(arr[in]>arr[in+1])
{
temp=arr[in];
arr[in]=arr[in+1];
arr[in+1]=temp;
}}}
System.out.print("\nAfter Sorting Array Element are: ");
for(i=0;i<size;i++)
System.out.print(arr[i]+" ");
cs.close();
}
}
public class Odd_Even_Sort {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int size;
System.out.print("Enter the size of the array:");
size=cs.nextInt();
int arr[]=new int[size],i;
System.out.print("Enter the element of the array:");
for(i=0;i<size;i++)
arr[i]=cs.nextInt();
System.out.print("Before Sorting Array Element are: ");
for(i=0;i<size;i++)
System.out.print(arr[i]+" ");
int out,in,temp;
for(out=0;out<size;out++){
//Even
for(in=0;in<size-1;in=in+2)
{
if(in!=size-1)
if(arr[in]>arr[in+1])
{
temp=arr[in];
arr[in]=arr[in+1];
arr[in+1]=temp;
}}
//odd
for(in=1;in<size-1;in=in+2)
{
if(in!=size-1)
if(arr[in]>arr[in+1])
{
temp=arr[in];
arr[in]=arr[in+1];
arr[in+1]=temp;
}}}
System.out.print("\nAfter Sorting Array Element are: ");
for(i=0;i<size;i++)
System.out.print(arr[i]+" ");
cs.close();
}
}
Input/Output:
Enter the size of the array:4
Enter the element of the array:3
2
2
6
Before Sorting Array Elements are: 3 2 2 6
After Sorting Array Elements are: 2 2 3 6
Enter the element of the array:3
2
2
6
Before Sorting Array Elements are: 3 2 2 6
After Sorting Array Elements are: 2 2 3 6
Odd-Even Sort Program in python using list
Solution In Python
size=int(input("Enter the size of the array:"));
arr=[]
print("Enter the element of the array:");
for i in range(0,size):
num = int(input())
arr.append(num)
print("Before Sorting Array Element are: ",arr)
for out in range(0,size):
for inn in range(0, size-1,+2):
if inn != size-1:
if arr[ inn] > arr[inn +1]:
temp = arr[inn]
arr[inn]=arr[inn +1]
arr[inn +1]=temp
for inn in range(1, size - 1, +2):
if inn != size-1:
if arr[ inn] > arr[inn +1]:
temp = arr[inn]
arr[inn]=arr[inn +1]
arr[inn +1]=temp
print("\nAfter Sorting Array Element are: ",arr)
arr=[]
print("Enter the element of the array:");
for i in range(0,size):
num = int(input())
arr.append(num)
print("Before Sorting Array Element are: ",arr)
for out in range(0,size):
for inn in range(0, size-1,+2):
if inn != size-1:
if arr[ inn] > arr[inn +1]:
temp = arr[inn]
arr[inn]=arr[inn +1]
arr[inn +1]=temp
for inn in range(1, size - 1, +2):
if inn != size-1:
if arr[ inn] > arr[inn +1]:
temp = arr[inn]
arr[inn]=arr[inn +1]
arr[inn +1]=temp
print("\nAfter Sorting Array Element are: ",arr)
Input/Output:
Enter the size of the array:5
Enter the element of the array:
4
3
6
7
4
Before Sorting Array Element are: [4, 3, 6, 7, 4]
After Sorting Array Element are: [3, 4, 4, 6, 7]
Enter the element of the array:
4
3
6
7
4
Before Sorting Array Element are: [4, 3, 6, 7, 4]
After Sorting Array Element are: [3, 4, 4, 6, 7]
0 Comments
Please do not Enter any spam link in the comment box