Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Write a C++ program to implement selection sort #5331

Closed
harshraj8843 opened this issue Jan 16, 2024 · 8 comments · Fixed by #5769
Closed

Write a C++ program to implement selection sort #5331

harshraj8843 opened this issue Jan 16, 2024 · 8 comments · Fixed by #5769
Assignees
Labels
C++ cpp related closed closed issues/PRs good first issue Good for newcomers program

Comments

@harshraj8843
Copy link
Contributor

Description

Write a C++ program to implement selection sort

Selection sort is a sorting algorithm that selects the smallest element from an unsorted list in each iteration and places that element at the beginning of the unsorted list.

Pseudocode

procedure selection sort 
   list  : array of items
   n     : size of list

   for i = 1 to n - 1
   /* set current element as minimum*/
      min = i    
   
      /* check the element to be minimum */

      for j = i+1 to n 
         if list[j] < list[min] then
            min = j;
         end if
      end for

      /* swap the minimum element with the current element*/
      if indexMin != i  then
         swap list[min] and list[i]
      end if
   end for
    
end procedure

Example

Input  : [8, 5, 2, 6, 9, 3, 1, 4, 0, 7]
Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
How to contribute
  • Comment !assign to assign this issue to yourself
  • Fork this repository
  • Create a new branch
  • Save the solution in program/program/implement-selection-sort/ImplementSelectionSort.cpp
  • Commit the changes
  • Create a pull request
@harshraj8843 harshraj8843 added C++ cpp related good first issue Good for newcomers program labels Jan 16, 2024
@codinasion-bot
Copy link

👋🏻 Hey @harshraj8843

💖 Thanks for opening this issue 💖

A team member should be by to give feedback soon.

@codinasion-bot codinasion-bot bot added the triage Waiting for review label Jan 16, 2024
@Parvezkhan0
Copy link
Contributor

!assign

@codinasion-bot
Copy link

Hey @NeurantumData, this issue is already assigned to @Parvezkhan0! cc/ @codinasion/codinasion

@sricharan200
Copy link
Contributor

!assign

@codinasion-bot
Copy link

codinasion-bot bot commented Apr 9, 2024

Hey @sricharan200, this issue is already assigned to @Parvezkhan0! cc/ @codinasion/codinasion

@SanjuPSaji
Copy link

#include

void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; ++i) {
int minIndex = i;
for (int j = i + 1; j < n; ++j) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the minimum element with the current element
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}

void printArray(int arr[], int n) {
for (int i = 0; i < n; ++i) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}

int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);

std::cout << "Original array: ";
printArray(arr, n);

selectionSort(arr, n);

std::cout << "Sorted array: ";
printArray(arr, n);

    return 0;
}

@khizer-flow
Copy link
Contributor

!assign

@codinasion-bot
Copy link

Hey @khizer-flow, this issue is already assigned to @Parvezkhan0! cc/ @codinasion/codinasion

@codinasion-bot codinasion-bot bot added closed closed issues/PRs and removed triage Waiting for review labels Jun 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
C++ cpp related closed closed issues/PRs good first issue Good for newcomers program
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants