-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertion_sort.c
More file actions
50 lines (45 loc) · 954 Bytes
/
insertion_sort.c
File metadata and controls
50 lines (45 loc) · 954 Bytes
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
#include <stdio.h>
#include <stdlib.h>
//#define SIZE 100
void insertionSort(int a[SIZE], int n)
{
int i, j, item;
for (i = 1; i < n; i++)
{
item = a[i];
j = i - 1;
while (j >= 0 && a[j] > item)
{
a[j + 1] = a[j];
j = j - 1;
}
a[j + 1] = item;
}
}
int presortedElementUnique(int a[SIZE], int n)
{
int i;
for (i = 0; i < n - 1; i++)
{
if (a[i] == a[i + 1])
return 0;
}
return 1;
}
int main()
{
int i, size, flag;
printf("\nEnter the size of array: ");
scanf("%d", &size);
int a[size];
printf("\nEnter the elements of the array: \n");
for (i = 0; i < size; i++)
scanf("%d", &a[i]);
insertionSort(a, size);
flag = presortedElementUnique(a, size);
if (flag)
printf("\nAll the elements are unique\n");
else
printf("\nElements are not unique\n");
return 0;
}