Skip to content

Commit 371c0a4

Browse files
authored
Merge pull request #39 from ng29/master
Modified selection sort
2 parents efa06d8 + 0073695 commit 371c0a4

File tree

2 files changed

+64
-52
lines changed

2 files changed

+64
-52
lines changed

Sorting/Selectionsort.cpp

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
1-
//
2-
// Created by Tay on 10/10/17.
3-
//
4-
5-
void selectionSort(int table[], int size) {
6-
int min, tmp;
7-
for (int i = 0; i < size - 1; i++) {
8-
min = i;
9-
for (int j = i + 1; j < size; j++) {
10-
if (table[j] < table[min]) {
11-
min = j;
12-
}
13-
}
14-
if (min != i) {
15-
tmp = table[i];
16-
table[i] = table[min];
17-
table[min] = tmp;
18-
}
19-
for (int a = 0; a < 7; a++) {
20-
std::cout << table[a] << " ";
21-
22-
}
23-
std::cout << std::endl;
24-
}
1+
#include<iostream.h>
2+
#include<conio.h>
3+
void main()
4+
{
5+
clrscr();
6+
int size, arr[50], i, j, temp;
7+
cout<<"Enter Array Size : ";
8+
cin>>size;
9+
cout<<"Enter Array Elements : ";
10+
for(i=0; i<size; i++)
11+
{
12+
cin>>arr[i];
13+
}
14+
cout<<"Sorting array using selection sort...\n";
15+
for(i=0; i<size; i++)
16+
{
17+
for(j=i+1; j<size; j++)
18+
{
19+
if(arr[i]>arr[j])
20+
{
21+
temp=arr[i];
22+
arr[i]=arr[j];
23+
arr[j]=temp;
24+
}
25+
}
26+
}
27+
cout<<"Now the Array after sorting is :\n";
28+
for(i=0; i<size; i++)
29+
{
30+
cout<<arr[i]<<" ";
31+
}
32+
getch();
2533
}
26-
27-
int main() {
28-
int table[] = {13, 32, 43, 14, 25};
29-
selectionSort(table, 5);
30-
return 0;
31-
}

Sorting/bubblesort.cpp

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
1-
#include<bits/stdc++.h>
2-
using namespace std;
3-
4-
void bubbleSort(int a[], int s) {
5-
for (int i = 1; i < s; i++) {
6-
bool swapped = false;
7-
8-
for (int j = 0; j < s - i; j++) {
9-
if (a[j] > a[j + 1]) {
10-
std::swap(a[j], a[j + 1]);
11-
swapped = true;
12-
}
13-
}
14-
15-
if (!swapped) {
16-
return;
17-
}
1+
#include <stdio.h>
2+
3+
int main()
4+
{
5+
int array[100], n, c, d, swap;
6+
7+
printf("Enter number of elements\n");
8+
scanf("%d", &n);
9+
10+
printf("Enter %d integers\n", n);
11+
12+
for (c = 0; c < n; c++)
13+
scanf("%d", &array[c]);
14+
15+
for (c = 0 ; c < ( n - 1 ); c++)
16+
{
17+
for (d = 0 ; d < n - c - 1; d++)
18+
{
19+
if (array[d] > array[d+1]) /* For decreasing order use < */
20+
{
21+
swap = array[d];
22+
array[d] = array[d+1];
23+
array[d+1] = swap;
24+
}
1825
}
26+
}
27+
28+
printf("Sorted list in ascending order:\n");
29+
30+
for ( c = 0 ; c < n ; c++ )
31+
printf("%d\n", array[c]);
32+
33+
return 0;
1934
}
20-
21-
void main(){
22-
int a[]={13,32,43,14,25};
23-
bubbleSort(a,5);
24-
}

0 commit comments

Comments
 (0)