Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update readme.md #593

Merged
merged 5 commits into from
Feb 12, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 76 additions & 10 deletions Algorithms/Sorting/SelectionSort/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ end procedure
* [JavaScript](#javascript)
* [Go](#go)
* [Ruby](#ruby)
* [c#](#CSharp)

### Python
```python
Expand Down Expand Up @@ -162,6 +163,17 @@ public class SelectionSort {
```c
#include <stdio.h>

void swap(int *p,int *q)
{
//p=&n1 so p store the address of n1, so *p store the value of n1
//q=&n2 so q store the address of n2, so *q store the value of n2

int tmp;
tmp = *p; // tmp store the value of n1
*p=*q; // *p store the value of *q that is value of n2
*q=tmp; // *q store the value of tmp that is the value of n1
}

void selectionSort(int arr[], int n)
{
int i, j, min_idx;
Expand All @@ -188,27 +200,25 @@ void selectionSortDesc(int arr[], int n)
}
}

void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("n");
printf("\n");
}

int main()
{
int arr[] = {64, 25, 12, 22, 11};
int arr[] = {64, 100, 2, 0, -1, 10};
int n = sizeof(arr)/sizeof(arr[0]);
printf("Before Sorting: ");
printArray(arr, n);
selectionSort(arr, n);
printf("Sorted array: n");
printf("Sorted array Ascending: ");
printArray(arr, n);
selectionSortDesc(arr, n);
printf("Sorted array Decending: ");
printArray(arr, n);
return 0;
}
Expand Down Expand Up @@ -330,4 +340,60 @@ arr = [64, 25, 12, 22, 11]
p selection_sort(arr) # [11, 12, 22, 25, 64]
p selection_sort_desc(arr) # [64, 25, 22, 12, 11]
```
### CSharp
```c#
using System;

class Program {

//sorting algorithm
public static void Sort<T>(T[] array) where T : IComparable
{
for (int i = 0; i < array.Length - 1; i++)
{
int minin = i;
T minval = array[i];
for (int z = i + 1; z < array.Length; z++)
{
if (array[z].CompareTo(minval) < 0)
{
minin = z;
minval = array[z];
}
}
Swap(array, i, minin);
}
}

//swaping algorithm
private static void Swap<T>(T[] array, int first, int second)
{
T tnum = array[first];
array[first] = array[second];
array[second] = tnum;
}

//code to print arrays
public static void print(int[] arr, int n)
{
for (int i = 0; i < n; i++)
Console.Write(arr[i] + " ");
}

//main program
public static void Main (string[] args) {
int[] arr = { 170, 45, 75, 90, 802, 24, 2, 66 };
int n = arr.Length;
Console.WriteLine("Starting array");
print(arr,n);


// Function Call
Sort(arr);
Console.WriteLine();
Console.WriteLine("Sorted array");
print(arr, n);
}
}
```