Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Programming Lab I

SORTING
<<
Diyah Utami Kusumaning Putri, S.Kom., M.Sc., M.Cs.
Sorting data is an important thing in real life to facilitate data
arrangement. Sorting can be done in ascending or
descending order.

Data 5 3 7 2 0 9 4 1 8 6

Ascending 0 1 2 3 4 5 6 7 8 9

Descending 9 8 7 6 5 4 3 2 1 0
Insertion Sort
The insertion sort method is a sorting method
that takes an insert data on the sorted data and
shifts the data that is larger than the insert
data so that the insert data can be placed in
the correct place.
Insertion Sort

Data 5 3 7 2 0 9 4 1 8 6

Insert Data = 3 5 3 7 2 0 9 4 1 8 6

Insert Data = 7 3 5 7 2 0 9 4 1 8 6

Insert Data = 2 3 5 7 2 0 9 4 1 8 6

Insert Data = 0 2 3 5 7 0 9 4 1 8 6

Insert Data = 9 0 2 3 5 7 9 4 1 8 6
Insertion Sort

Insert Data = 4 0 2 3 5 7 9 4 1 8 6

Insert Data = 1 0 2 3 4 5 7 9 1 8 6

Insert Data = 8 0 1 2 3 4 5 7 9 8 6

Insert Data = 6 0 1 2 3 4 5 7 8 9 6

Result 0 1 2 3 4 5 6 7 8 9
Insertion Sort
#include <iostream>
using namespace std;

int main() {
int data[] = {5,3,7,2,0,9,4,1,8,6};
int i, dataSize=sizeof(data)/sizeof(data[0]), temp;

for(int j = 1; j<dataSize; j++) {


i = j-1;
temp=data[j];
while(data[i]>temp && i>=0){
data[i+1] = data[i];
i--;
}
data[i+1] = temp;
}

for(int j = 0;j<dataSize;j++){
cout<<data[j]<<" ";
}

return 0;
}
Selection Sort
The selection sort method is a sorting method
that searches the minimum value (or maximum
value depending on the ascending or descending
sort) which is then placed in the frontmost
position, then searches for the second or
largest value again along with the number of
array elements one. Once found, the second
element is exchanged for that value, and so on.
Selection Sort

Data 5 3 7 2 0 9 4 1 8 6

Minimum = 0 5 3 7 2 0 9 4 1 8 6

Minimum = 1 0 3 7 2 5 9 4 1 8 6

Minimum = 2 0 1 7 2 5 9 4 3 8 6

Minimum = 3 0 1 2 7 5 9 4 3 8 6

Minimum = 4 0 1 2 3 5 9 4 7 8 6
Selection Sort

Minimum = 5 0 1 2 3 4 9 5 7 8 6

Minimum = 6 0 1 2 3 4 5 9 7 8 6

Minimum = 7 0 1 2 3 4 5 6 7 8 9

Minimum = 8 0 1 2 3 4 5 6 7 8 9

Result 0 1 2 3 4 5 6 7 8 9
Selection Sort
#include <iostream>
using namespace std;

int main() {
int data[] = {5,3,7,2,0,9,4,1,8,6};
int dataSize=sizeof(data)/sizeof(data[0]), min, temp;

for(int i = 0; i<dataSize; i++) {


min = i;
for(int j=1+i; j<dataSize; j++){
if(data[j] < data[min]){
min = j;
}
}
temp = data[i];
data[i] = data[min];
data[min] = temp;
}

for(int i = 0;i<dataSize;i++){
cout<<data[i]<<" ";
}
return 0;
}
Bubble Sort
The bubble sort method is a sorting method that
exchanges two elements continuously until the
sorting is complete.
Bubble Sort
5 3 7 2

5 3 7 2

3 5 7 2

3 5 7 2

3 5 2 7

3 5 2 7

3 2 5 7
Bubble Sort

3 2 5 7

2 3 5 7

2 3 5 7

2 3 5 7
Bubble Sort
#include <iostream>
using namespace std;

int main() {
int data[] = {5,3,7,2,0,9,4,1,8,6};
int dataSize=sizeof(data)/sizeof(data[0]), temp;
for(int i = 0; i<dataSize-1; i++) {
for(int j= 0; j<dataSize-1; j++){
if(data[j] > data[j+1]){
temp = data[j] ;
data[j] = data[j+1];
data[j+1] = temp;
}
}
}

for(int i = 0;i<dataSize;i++){
cout<<data[i]<<" ";
}

return 0;
}
“Life is trying things to see if
they work”

—Ray Bradbury

You might also like