Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ad65351

Browse files
editesselectionsortinc
1 parent f722f13 commit ad65351

File tree

1 file changed

+44
-0
lines changed
  • javascript/interview-techdev-guide-master/Algorithms/Searching & Sorting/Selection Sort

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
void swap(int *xp, int *yp)
2+
{
3+
int temp = *xp;
4+
*xp = *yp;
5+
*yp = temp;
6+
}
7+
8+
void selectionSort(int arr[], int n)
9+
{
10+
int i, j, min_idx;
11+
12+
// One by one move boundary of unsorted subarray
13+
for (i = 0; i < n-1; i++)
14+
{
15+
// Find the minimum element in unsorted array
16+
min_idx = i;
17+
for (j = i+1; j < n; j++)
18+
if (arr[j] < arr[min_idx])
19+
min_idx = j;
20+
21+
// Swap the found minimum element with the first element
22+
swap(&arr[min_idx], &arr[i]);
23+
}
24+
}
25+
26+
/* Function to print an array */
27+
void printArray(int arr[], int size)
28+
{
29+
int i;
30+
for (i=0; i < size; i++)
31+
printf("%d ", arr[i]);
32+
printf("\n");
33+
}
34+
35+
// Driver program to test above functions
36+
int main()
37+
{
38+
int arr[] = {64, 25, 12, 22, 11};
39+
int n = sizeof(arr)/sizeof(arr[0]);
40+
selectionSort(arr, n);
41+
printf("Sorted array: \n");
42+
printArray(arr, n);
43+
return 0;
44+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /