Skip to content

Instantly share code, notes, and snippets.

View shameemreza's full-sized avatar
🦜
Talk is cheap. Check My Code, Blog and Portfolio.

Shameem Reza shameemreza

🦜
Talk is cheap. Check My Code, Blog and Portfolio.
View GitHub Profile
Selected Item: i5 Weight: 1 Value: 3 Total Weight: 1.000000 Total Benefit: 3.000000
Selected Item: i6 Weight: 3 Value: 5 Total Weight: 4.000000 Total Benefit: 8.000000
Selected Item: i4 Weight: 5 Value: 8 Total Weight: 9.000000 Total Benefit: 16.000000
Selected Item: i1 Weight: 6 Value: 6 Total Weight: 15.000000 Total Benefit: 22.000000
Selected Item: i3 Weight: 1 Value: 0.333333 Total Weight: 16.000000 Total Benefit: 22.333334
Total Weight: 16.000000
Total Benefit: 22.333334
for (int i=1; i<=n; i++) {
for (int j=i+1; j<=n; j++) {
if(num[i]>num[j]){
//swap is inside algorithm header.
swap(num[i], num[j]);
}
}
}
// C program for implementation of selection sort
#include <stdio.h>
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
Sorted array: 11 12 22 25 64
# Python program for implementation of Selection
# Sort
import sys
A = [64, 25, 12, 22, 11]
# Traverse through all array elements
for i in range(len(A)):
# Find the minimum element in remaining
# unsorted array
Sorted array: 11 12 22 25 64
begin BubbleSort(list)
for all elements of list
if list[i] > list[i+1]
swap(list[i], list[i+1])
end if
end for
return list
for(i=1; i<=n; i++)
{
for (j=1; j<n; j++)
if(num[j+1] > num[j])
{
temp=num[j];
num[j] = num[j+1];
num[j+1] = temp;
}
}
// C program for implementation of Bubble sort
#include <stdio.h>
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
# Python program for implementation of Bubble Sort
def bubbleSort(arr):
n = len(arr)
# Traverse through all array elements
for i in range(n):
# Last i elements are already in place
for j in range(0, n-i-1):