logo

Sorting Algorithms

   

Added on  2023-03-20

7 Pages968 Words43 Views
Running head: SORTING ALGORITHMS
SORTING ALGORITHMS
Name of the Student
Name of the University
Author Note
Sorting Algorithms_1
1SORTING ALGORITHMS
Introduction:
In this particular assignment the objective is to implement different sorting algorithms in
MATLAB and measure their performances. Sorting is basically arranging a given dataset or
vector in either ascending or descending order. This can be performed by different ways or by
different algorithms. The algorithms are Selection sort, Bubble Sort, Insertion sort, Merge
sort, Quick sort, Heap sort, Counting sort, Radix sort, Bucket sort, Shell short, TimSort,
Comb Sort, Pigeonhole Sort, Cycle Sort, Cocktail Sort, Bitonic Sort, Pancake sorting, Gnome
Sort, Sleep Sort, Stooge Sort and more sorting techniques based on place of application are
being developed day by day. In this assignment the focus is on the primary two algorithms
which are Selection sort and Bubble sort which are implemented in MATLAB. Both sorting
are performed in ascending manner i.e. the display of the sorted data is in ascending order.
Selection sort Algorithm:
In this method first the minimum of the entire data row of size (n*1) and its position is
calculated. Then the minimum is swapped with the leftmost data point. Then this process is
repeated for data row of (n-1)*1 size i.e. process is continued with entire data except leaving
the leftmost data point. Finally, this iterative process ends with operation on the two right
most data points and data is sorted from left to right in ascending order.
MATLAB function for Selection sort:
function sorted = selectionsort(a)
%%%
% Performs sorting by Selection sorting algorithm on the given row vector.
% Input = User input array
Sorting Algorithms_2
2SORTING ALGORITHMS
% Output = Output array in ascending order
%%%
%a = [-5 1 4 -2 8 1 6 9 20 19]; % input vector of digits
sorted = a; % initializing sorted vector with a
for i=1:length(a)-1
[m,ipos] = min(sorted(i:length(a))); % extracting minimum and minimum index position
minipos = ipos + i-1; % adjusting minimum index position w.r.t vector A
%%% swapping betweeen found minimum and the number in ith position
%------
c = sorted(i); %
sorted(i) = sorted(minipos);
sorted(minipos) = c;
%------
end
end
Output with sample run:
a = [-5 1 4 -2 8 1 6 9 20 19];
>> sorted = selectionsort(a)
sorted =
-5 -2 1 1 4 6 8 9 19 20
Sorting Algorithms_3

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents
THE ARTIFICIAL INTELLIGENCE
|9
|1495
|22

THE ARTIFICIAL INTELLIGENCE
|9
|1215
|17

Importing and Presenting Data in MATLAB
|6
|1081
|215

Algorithms for Searching, Sorting, and Matrix Operations
|8
|1756
|131

Merge Sort Algorithm Analysis
|20
|3132
|309

Sorting and Search Algorithms in C Programming
|11
|1306
|352