-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.m
More file actions
156 lines (127 loc) · 5.89 KB
/
Copy pathmain.m
File metadata and controls
156 lines (127 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
% Perform insertion, bubble and merge sorting algorithms.
% 5 October 2021.
% Written by Orhan Ozan Yildiz.
%% Clear memory and screen.
clear, clc, close all;
flag = true;
while flag
%% Where the user is expected to decide how to enter the array of numbers.
disp('Press 1 if you want to use a random array,')
disp('press 2 if you want to decide the array, and')
disp('press 3 if you want to compare sorting algorithms')
disp('press 4 if you want to exit.')
chosenInput = input('please select one: ');
%% Controller section. Arrays are sorted according to the decision made by the user.
if chosenInput == 1
% The user has to decide the range of the array.
array = randiArray();
disp(array)
% Calling sorting functions.
callingSorting(array)
elseif chosenInput == 2
% The user creates the array.
userArray = input('Please enter numbers using commas i.e [k,w,x,y,z]: ');
disp(userArray)
% Calling sorting functions.
callingSorting(userArray)
elseif chosenInput == 3
elements = {};
times_insertion = {};
times_merge = {};
times_merge_insert = {};
times_bubble = {};
times_quick = {};
times_heap = {};
times_counting = {};
times_radix = {};
times_bucket = {};
for i = 1:10
% Generate some integers.
arr = randi([0,10000*i], 1000*i);
elements = [elements, length(arr)];
% For insertion sorting.
[insertion_sorting, ins_comp_time] = insertion(arr);
disp([length(arr), " Elements Sorted by InsertionSort in ", ins_comp_time])
times_insertion = [times_insertion, ins_comp_time];
% For bubble sorting.
[bubble_sorting, bub_comp_time] = bubble(arr);
disp([length(arr), " Elements Sorted by BubbleSort in ", bub_comp_time])
times_bubble = [times_bubble, bub_comp_time];
% For merge sorting.
[merge_sorting, merg_comp_time] = merge(arr,1,length(arr));
disp([length(arr), " Elements Sorted by MergeSort in ", merg_comp_time])
times_merge = [times_merge, merg_comp_time];
% For quick sorting.
[quick_sorting, quick_comp_time] = quick(arr);
disp([length(arr), " Elements Sorted by QuickSort in ", quick_comp_time])
times_quick = [times_quick, quick_comp_time];
% For heap sorting.
[heap_sorting, heap_comp_time] = heap(arr);
disp([length(arr), " Elements Sorted by HeapSort in ", heap_comp_time])
times_heap = [times_heap, heap_comp_time];
% For counting sorting.
[counting_sorting, counting_comp_time] = counting(arr);
disp([length(arr), " Elements Sorted by CountingSort in ", counting_comp_time])
times_counting = [times_counting, counting_comp_time];
% For radix sorting.
[radix_sorting, radix_comp_time] = radix(arr);
disp([length(arr), " Elements Sorted by RadixSort in ", radix_comp_time])
times_radix = [times_radix, radix_comp_time];
% For bucket sorting.
[bucket_sorting, bucket_comp_time] = bucket(arr);
disp([length(arr), " Elements Sorted by BucketSort in ", bucket_comp_time])
times_bucket = [times_bucket, bucket_comp_time];
end
plot(str2double(elements),str2double(times_insertion), 'b-','LineWidth', 2)
plot(str2double(elements),str2double(times_bubble),'b-','LineWidth', 2)
plot(str2double(elements),str2double(times_merge),'b-','LineWidth', 2)
title('2-D Line Plot for Comparasion Sorting Algorithms')
xlabel('computational time')
ylabel('Length of array')
elseif chosenInput == 4
flag = false;
break;
else
disp('This input not valid. Please try again.')
break;
end
end
function callingSorting(array)
% Calling sorting functions.
[insertion_sorting, ins_comp_time] = insertion(array);
disp('Insertion sort result: ');
disp(insertion_sorting)
[bubble_sorting, bub_comp_time] = bubble(array);
disp('Bubble sort result: ');
disp(bubble_sorting)
[merge_sorting, merg_comp_time] = merge(array,1,length(array));
disp('Merge sort result: ');
disp(merge_sorting)
[merge_insertion_sorting, merg_ins_comp_time] = merge_insertion(array);
disp('Merge insertion sort result: ')
disp(merge_insertion_sorting)
[heap_sorting, heap_comp_time] = heap(array);
disp('Heap sort result: ')
disp(heap_sorting)
[quick_sorting, quick_comp_time] = quick(array);
disp('Quick sort result: ')
disp(quick_sorting)
[counting_sorting, counting_comp_time] = counting(array);
disp('Counting sort result: ')
disp(counting_sorting)
[radix_sorting, radix_comp_time] = radix(array);
disp('Radix sort result: ')
disp(radix_sorting)
[bucket_sorting, bucket_comp_time] = bucket(array);
disp('Bucket sort result: ')
disp(bucket_sorting)
fprintf('insertion sort computational time is %.4f \n',ins_comp_time)
fprintf('bubble sort computational time is %.4f \n',bub_comp_time)
fprintf('merge sort computational time is %.4f \n',merg_comp_time)
fprintf('merge insertion sort computational time is %.4f \n',merg_ins_comp_time)
fprintf('heap sort computational time is %.4f \n',heap_comp_time)
fprintf('quick sort computational time is %.4f \n',quick_comp_time)
fprintf('counting sort computational time is %.4f \n',counting_comp_time)
fprintf('radix sort computational time is %.4f \n',radix_comp_time)
fprintf('bucket sort computational time is %.4f \n',bucket_comp_time)
end