NEP -DAA -DESIGN AND ANALYSIS OF ALOGRITHM

PROGRAM 1 PROGRAM 2 PROGRAM 3 PROGRAM 4 PROGRAM 5 PROGRAM 6 PROGRAM 7 PROGRAM 8 PROGRAM 9 PROGRAM 10 PROGRAM 11 PROGRAM 12

PART B

PROGRAM B1 PROGRAM B2 PROGRAM B3 PROGRAM B4 PROGRAM B5 PROGRAM B6 PROGRAM B7 PROGRAM B8 . . .

 
    
 # function to sort array using selection sort
selection_sort <- function(x)
{
	# length of array
	n <- length(x)
	for (i in 1 : (n - 1))
	{
		# assume element at i is minimum
		min_index <- i
		for (j in (i + 1) : (n))
		{
			# check if element at j is smaller 
			# than element at min_index
			if (x[j] < x[min_index]) {
				# if yes, update min_index
				min_index = j
			}
		}
		# swap element at i with element at min_index 
		temp <- x[i]
		x[i] <- x[min_index]
		x[min_index] <- temp
	}
	x
}

# take sample input
arr <- sample(1 : 100, 10)

# sort array 
sorted_arr <- selection_sort(arr)

# print array
print(sorted_arr)