Skip to content

Paul-Rajeevan-Nimalarajah/selection-sort-algorithm-in-Python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

selection-sort-algorithm-in-Python

Selection Sort in Python This repository contains a Python implementation of the selection sort algorithm. The program sorts a list of numbers (integers or floats) provided by the user and tracks the original indices of the numbers as they are sorted. It also displays the intermediate states of the list and indices after each iteration, making it a great tool for understanding how selection sort works step-by-step. Features

Accepts comma-separated numbers as input (e.g., 5,2,8,1.5). Handles both integers and floating-point numbers. Ignores invalid inputs (e.g., letters or symbols). Tracks and updates the original indices of the numbers during sorting. Prints the list and indices after each iteration, along with a final sorted result. Counts and displays the number of iterations performed.

How It Works The program uses the selection sort algorithm, which repeatedly finds the minimum element in the unsorted portion of the list and swaps it with the first unsorted element. Alongside sorting the numbers, it maintains a list of indices to track where each number was originally located. The intermediate and final states are printed to help visualize the sorting process. Example Usage

Run the program. Enter numbers separated by commas when prompted (e.g., 5,2,8,1.5). The program will display: The updated list and indices after each iteration. The final sorted list, sorted indices, and total iteration count.

Sample Input: Enter numbers separated by commas: 5,2,8,1.5

Sample Output: Loop count = 1 Updated list: [1.5, 2, 8, 5] Updated index: [3, 1, 2, 0]

Loop count = 2 Updated list: [1.5, 2, 8, 5] Updated index: [3, 1, 2, 0]

Loop count = 3 Updated list: [1.5, 2, 5, 8] Updated index: [3, 1, 0, 2]

Loop count = 4 Sorted list: [1.5, 2, 5, 8] Sorted index: [3, 1, 0, 2]

Requirements

Python 3.x (no external libraries required).

Installation

Clone the repository:git clone https://github.com/Paul-Rajeevan-Nimalarajah/selection-sort-algorithm-in-Python.git

Navigate to the repository folder:cd selection-sort-algorithm-in-Python

Run the program:python selection_sort.py

Code Structure

selection_sorting(): The main function that: Collects user input as a comma-separated string. Converts valid inputs to numbers (integers or floats). Creates an index list to track original positions. Performs selection sort while updating numbers and indices. Prints intermediate and final states.

Limitations

Does not handle empty input lists (e.g., entering no valid numbers). Does not support negative numbers explicitly (though they work fine). Output formatting could be improved for better readability.

Future Improvements

Add input validation for empty or single-element lists. Enhance output formatting (e.g., use tables or aligned columns). Add support for sorting in descending order. Include a visualization of the sorting process (e.g., using matplotlib or a GUI).

Contributing Contributions are welcome! Feel free to open issues or submit pull requests to improve the code, add features, or fix bugs. Please follow standard GitHub contribution guidelines. License This project is licensed under the MIT License. See the LICENSE file for details. Author Paul Rajeevan – Feel free to connect with me on GitHub!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages