header ads

Sorting of array | C, C++, etc | Method of Sorting of array

 Sorting of an Array


Given an array arr[] of size N, the task is to sort this array in ascending order in 

C, C++.or any language.


Method of Sorting of Array.

Examples: 


Input: arr[] = {0, 23, 14, 12, 9}
Output: {0, 9, 12, 14, 23}

Input: arr[] = {7, 0, 2}
Output: {0, 2, 7}
Simple and Very Easy Method
#include <iostream>                                                      
using namespace std;                                                     
int main()                                                               
{                                                                        
//	show element of integer array in assending and             desending order                                                          
	int arr[10];                                                     
for (int i = 0; i < 10; i++)                                             
{                                                                        
	cout<<"Enter Element at "<<i + 1<<" Position: ";                  
	cin>>arr[i];	                                                  
}                                                                         
                                                                         
for (int i = 0; i < 10; i++)                                             
{                                                                        
	for (int j = i + 1; j < 10; j++)                                 
	{                                                                
		if(arr[i] <= arr[j])                                      
		{                                                         
			continue;                                         
		}                                                         
		else                                                      
		{                                                         
			int temp;                                         
			temp = arr[i];                                    
			arr[i] = arr[j];                                  
			arr[j] = temp;                                    
		}                                                         
	}                                                                 
}                                                                         
for (int i = 0; i < 10; i++)                                              
{                                                                         
	cout<<arr[i]<<", ";	                                           
}	                                                                   
	return 0;                                                          
}                                                                          
                 

Output:
Enter Element at 1 Position: 23
Enter Element at 2 Position: 45
Enter Element at 3 Position: 23
Enter Element at 4 Position: 64
Enter Element at 5 Position: 27
Enter Element at 6 Position: 77
Enter Element at 7 Position: 55
Enter Element at 8 Position: 34
Enter Element at 9 Position: 13
Enter Element at 10 Position: 89
13, 23, 23, 27, 34, 45, 55, 64, 77, 89

Post a Comment

0 Comments