-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from hassamkhan1/contribute-opensource
Create selection-sort
- Loading branch information
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |