ADVERTISEMENT
ADVERTISEMENT

Rust program to sort an array in descending order using selection sort

Rust | Array Sorting: Write a program to sort an array in descending order using selection sort.
Submitted by Nidhi, on October 22, 2021

Problem Solution:

In this program, we will create an array of integer elements then we will sort created the array in descending order using selection sort.

Program/Source Code:

The source code to sort an array in descending order using selection sort is given below. The given program is compiled and executed successfully.

// Rust program to sort an array in descending order 
// using selection sort

fn main() 
{
    	let mut arr:[usize;5] = [5,1,23,11,26];
    	let mut i:usize=0;
    	let mut j:usize=0;
    
    	let mut min:usize=0;
    	let mut temp:usize=0;
    
    	println!("Array before sorting: {:?}",arr);

    	while i <= 4 
    	{
		min = i;
		j   = i + 1; 
		
		while j <= 4 
		{
			if arr[j] > arr[min] {
				min = j;
			}
			j=j+1; 
		}
		temp = arr[i];
		arr[i] = arr[min];
		arr[min] = temp;
		
		i=i+1;
	}
	println!("Array after sorting: {:?}",arr);
}

Output:

Array before sorting: [5, 1, 23, 11, 26]
Array after sorting: [26, 23, 11, 5, 1]

Explanation:

Here, we created an array of integers with 5 elements and then we sorted the created array in descending order using selection sort. After that, we printed the sorted array.

Rust Arrays Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.