Skip to content

Commit

Permalink
Merge pull request #28 from hassamkhan1/contribute-opensource
Browse files Browse the repository at this point in the history
Create selection-sort
  • Loading branch information
awaissaddiqui authored Oct 19, 2024
2 parents 191a7b6 + 42e8e6f commit 2f5e0f5
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
50 changes: 50 additions & 0 deletions C++/selection-sort
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// C++ program to implement Selection Sort
#include <bits/stdc++.h>
using namespace std;

void selectionSort(vector<int> &arr) {
int n = arr.size();

for (int i = 0; i < n - 1; ++i) {

// Assume the current position holds
// the minimum element
int min_idx = i;

// Iterate through the unsorted portion
// to find the actual minimum
for (int j = i + 1; j < n; ++j) {
if (arr[j] < arr[min_idx]) {

// Update min_idx if a smaller
// element is found
min_idx = j;
}
}

// Move minimum element to its
// correct position
swap(arr[i], arr[min_idx]);
}
}

void printArray(vector<int> &arr) {
for (int &val : arr) {
cout << val << " ";
}
cout << endl;
}

int main() {
vector<int> arr = {64, 25, 12, 22, 11};

cout << "Original array: ";
printArray(arr);

selectionSort(arr);

cout << "Sorted array: ";
printArray(arr);

return 0;
}
30 changes: 30 additions & 0 deletions Java/vector-list
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Java program to
// convert vector to List

import java.util.*;

public class GFG {
public static void main(String[] args)
{

// Create a Vector of String elements
Vector<String> vec = new Vector<String>();

// Adding values of Vector
vec.add("1");
vec.add("2");
vec.add("3");
vec.add("4");
vec.add("5");

// print Vector elements
System.out.println("Vector: " + vec);

// Convert Vector to List
List<String>
list = Collections.list(vec.elements());

// print List Elements
System.out.println("List:" + list);
}
}

0 comments on commit 2f5e0f5

Please sign in to comment.