-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort-Selection.cpp
More file actions
54 lines (45 loc) · 982 Bytes
/
Sort-Selection.cpp
File metadata and controls
54 lines (45 loc) · 982 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//iterative aproach to implement selection sort in ascending order
#include <iostream>
using namespace std;
void swap(int *ptr1, int *ptr2)
{
int temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
}
void sortArray(int arr[], int size)
{
int i,j,min_index;
for(i = 0 ; i<size-1 ; i++)
{
min_index = i;
for(j = i+1 ; j<size ; j++)
{
if(arr[j] < arr[min_index])
{
min_index = j;
}
swap(&arr[min_index], &arr[i]);
}
}
}
void printArray(int *ptrArray, int size)
{
for(int i=0 ; i<size ; i++)
{
cout << *ptrArray << " ";
ptrArray++;
}
cout << endl;
}
int main()
{
int arr[] = {34,12,56,10,11};
int size = sizeof(arr)/sizeof(arr[0]);
cout << "Printing Array in Unsorted form : ";
printArray(arr,size);
sortArray(arr, size);
cout << "Pinting Array in Sorted form : ";
printArray(arr,size);
return 0;
}