0% found this document useful (0 votes)
69 views

Insertion Sort

The document contains code for implementing various data structures in C including: 1) Insertion sort, selection sort, and bubble sort algorithms for sorting arrays. 2) Code for a linked list data structure with functions for insertion, deletion, searching, and printing elements. 3) Code for a stack data structure with push, pop, and print functions. 4) Code for a queue data structure with push, pop, and print functions. 5) Code for a complete binary tree data structure with functions for insertion, searching, traversing ancestors, calculating height, and finding siblings.

Uploaded by

Mungunsuvd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Insertion Sort

The document contains code for implementing various data structures in C including: 1) Insertion sort, selection sort, and bubble sort algorithms for sorting arrays. 2) Code for a linked list data structure with functions for insertion, deletion, searching, and printing elements. 3) Code for a stack data structure with push, pop, and print functions. 4) Code for a queue data structure with push, pop, and print functions. 5) Code for a complete binary tree data structure with functions for insertion, searching, traversing ancestors, calculating height, and finding siblings.

Uploaded by

Mungunsuvd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

1.

Insertion sort
#include<stdio.h>
int main(){
int i, j, n, temp, a[100];

printf("Husnegtiin hemjee oruulna uu?: ");


scanf("%d",&n);
printf("%d elementee oruulna uu: ", n);

for(i=0;i<n;i++)
scanf("%d",&a[i]);

for(i=1;i<n;i++){
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0)){
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}

printf("Eremblegdsen element ");


for(i=0;i<n;i++)
printf(" %d",a[i]);

return 0;
}

Selection sort
#include<stdio.h>
int main(){

int i, j, n, temp, a[100];

printf("Husnegtiin hemjee oruulna uu");


scanf("%d",&n);
printf("%d elementee oruulna uu ", n);

for(i=0;i<n;i++)
scanf("%d",&a[i]);

for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}

printf("Eremblegdsen element");
for(i=0;i<n;i++)
printf(" %d",a[i]);

return 0;
}

Bubble sort
#include <stdio.h>

int main()
{
int a[20], n, i, j, temp;
printf("Husnegtiin hemjee oruulna uu ");
scanf("%d", &n);
printf("%d elementee oruulna uu");
for (i = 0; i < n; ++i)
scanf("%d", &a[i]);
for (i = 1; i < n; ++i)
for (j = 0; j < (n - i); ++j)
if (a[j] > a[j + 1])
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
printf("\n Eremblegdsen element");
for (i = 0; i < n; ++i)
printf("%d ", a[i]);

return 0;
}

2. List
#include <stdio.h>

struct Array
{
int a[100];
int len;
};

struct List
{
struct Array list;
};
typedef struct List List;
#define l_arr list.a
#define l_len list.len

void l_insert(List *, int, int);


void l_erase(List *, int);
void l_print(List *);
int l_search(List *, int);

int main()
{
int x, t, ds, pos;
List l;
printf("1: hevleh 2:Haih, 3: oruulah, "
"4:gargah, \n");
scanf("%d", &t);

switch (t)
{
case 1:
l_print(&l);
printf("Oruulax utga: ");
scanf("%d", &x);

break;
case 2:
printf("Xaix utga: ");
scanf("%d", &x);
printf("Oldson bairlal: %d\n", l_search(&l, x));

break;
case 3:
printf("Oruulax utga: ");
scanf("%d", &x);
printf("Oruulax bairlal: ");
scanf("%d", &pos);
l_insert(&l, x, pos);

break;
case 4:

printf("Gargax bairlal: ");


scanf("%d", &pos);
l_erase(&l, pos);

break;

default:
break;
}
}
// }
// return 0;
// }

void l_insert(List *p, int x, int pos)


{

if (pos >= p->l_len)


{
p->l_a[p->l_len] = x;
}
else
{
for (int i = p->l_len; i > pos; i--)
{
p->l_a[i] = p->l_a[i - 1];
}
p->l_a[pos] = x;
}
p->l_len++;
}

void l_erase(List *p, int pos)


{

if (pos < p->l_len)


{
for (int i = pos; i < p->l_len - 1; i++)
{
p->l_a[i] = p->l_a[i + 1];
}
p->l_len--;
}
}

void l_print(List *p)


{
int i;
for (i = 0; i < p->l_len; i++)
{
printf("%d ", p->l_a[i]);
}
printf("\n");
}

int l_search(List *p, int x)


{
for (int i = 0; i < p->l_len; i++)
{
if (p->l_a[i] == x)
{
return i;
}
}
return -1;
}
a) Хэвлэх

2) Хайх

3) Оруулах

4) Гаргах

3. Stack
#include <stdio.h>

struct Array
{
int a[100];
int len;
};

struct Stack
{
struct Array stack;
};
typedef struct Stack Stack;
#define s_array stack.a
#define s_len stack.len

void s_push(Stack *, int);


void s_pop(Stack *);
void s_print(Stack *);

int main()
{
int x, t, ds, pos;

Stack s;

int done = 0;
while (!done)
{
printf("1: push (back), 2: pop (back), 3: print, "
"4: haah, \n");
scanf("%d", &t);

switch (t)
{
case 1:
printf("Oruulax utga: ");
scanf("%d", &x);
s_push(&s, x);

break;
case 2:

s_pop(&s);

break;
case 3:

s_print(&s);
break;

case 4:
done = 1;
break;
default:
break;
}
}
}
void s_push(Stack *p, int x)
{
p->s_array[p->s_len] = x;
p->s_len++;
}

void s_pop(Stack *p)


{
p->s_len--;
}

void s_print(Stack *p)


{
int i;
for (i = 0; i < p->s_len; i++)
{
printf("%d ", p->s_array[i]);
}
printf("\n");
}
a) Push

b) Pop c) Print
4. Queue
#include <stdio.h>

struct Array
{
int a[100];
int len;
};

struct Queue
{
struct Array queue;
};
typedef struct Queue Queue;
#define q_array queue.a
#define q_len queue.len
void q_push(Queue *, int);
void q_pop(Queue *);
void q_print(Queue *);

int main()
{
int x, t, pos;

Queue q;

int done = 0;
while (!done)
{
printf("1:hevleh, 2: gargah, 3: oruulah "
"4: haah, \n");
scanf("%d", &t);

switch (t)
{
case 1:
printf("Oruulax utga: ");
scanf("%d", &x);
q_print(&q);
break;
case 2:
q_pop(&q);

case 3:
printf("Gargah utga: ");
scanf("%d", &x);
q_push(&q, x);

case 4:
done = 1;
break;
default:
break;
}
}
}

void q_push(Queue *p, int x)


{

p->q_array[p->q_len] = x;
p->q_len++;
}

void q_pop(Queue *p)


{
for (int i = 0; i < p->q_len - 1; i++)
{
p->q_array[i] = p->q_arr[i + 1];
}
p->q_len--;
}

void q_print(Queue *p)


{
int i;
for (i = 0; i < p->q_len; i++)
{
printf("%d ", p->q_array[i]);
}
printf("\n");
}
a) Хэвлэх
b)Гаргах

c) Оруулах
5. Complete binary tree

/*
p-ийн зааж буй CBTree-д x утгыг оруулна
*/
void cb_push(CBTree *p, int x)
{
/* Энд оруулах үйлдлийг хийнэ үү */
p->cb_array[p->cb_len] = x;
p->cb_len++;
}

/*
p-ийн зааж буй CBTree-д idx индекстэй оройны зүүн хүүгийн индексийг
буцаана.
Зүүн хүү байхгүй бол -1 буцаана.
*/
int cb_left(const CBTree *p, int idx)
{
/* Энд хүүхдүүдийн утгыг хэвлэх үйлдлийг хийнэ үү */
if (p->cb_len > idx * 2 + 1)
{
return idx * 2 + 1;
}
else
{
return -1;
}
}

/*
p-ийн зааж буй CBTree-д idx индекстэй оройны баруун хүүгийн индексийг
буцаана.
Баруун хүү байхгүй бол -1 буцаана.
*/
int cb_right(const CBTree *p, int idx)
{

/* Энд хүүхдүүдийн утгыг хэвлэх үйлдлийг хийнэ үү */


if (p->cb_len > idx * 2 + 2)
{
return idx * 2 + 2;
}
else
{
return -1;
}
}
/*
p-ийн зааж буй CBTree-с k тоог хайн
хамгийн эхэнд олдсон индексийг буцаана.
Олдохгүй бол -1 утгыг буцаана.
*/
int cb_search(const CBTree *p, int k)
{
/* Энд хайх үйлдлийг хийнэ */
for (int i = 0; i < p->cb_len; i++)
{
if (p->cb_arr[i] == k)
{
return i;
}
}
return -1;
}

/*
p-ийн зааж буй CBTree-д idx индекстэй зангилаанаас дээшхи бүх өвөг
эцэгийг олох үйлдлийг хийнэ.
Тухайн орой өөрөө өвөг эцэгт орохгүй.
Өвөг эцэг бүрийг нэг шинэ мөрөнд хэвлэнэ. Өвөг эцэгийг доороос дээшхи
дарааллаар хэвлэнэ.
*/
void cb_ancestors(const CBTree *p, int idx)
{
/* Энд өвөг эцгийг олох үйлдлийг хийнэ үү */

if (idx >= 1)
{
idx = (idx - 1) / 2;
printf("%d\n", p->cb_arr[idx]);
cb_ancestors(p, idx);
}
}

/*
p-ийн зааж буй CBTree-ийн өндрийг буцаана
*/
int cb_height(const CBTree *p)
{
/* Энд өндрийг олох үйлдлийг хийнэ */
int height = 0, i = 0;
while (i < p->cb_len)
{
height++;
i = i * 2 + 1;
}
return height;
}

/*
p-ийн зааж буй CBTree-д idx оройны ах, дүү оройн дугаарыг буцаана.
Тухайн оройн эцэгтэй адил эцэгтэй орой.
Ах, дүү нь байхгүй бол -1-г буцаана.
*/
int cb_sibling(const CBTree *p, int idx)
{
/* Энд ах, дүүг олох үйлдлийг хийнэ үү */
if (idx == 0)
{
return -1;
}
int ancestorIdx;
int right, left;
ancestorIdx = (idx - 1) / 2;
left = ancestorIdx * 2 + 1;
right = ancestorIdx * 2 + 2;
if (right < p->cb_len)
{
if (right != idx)
{
return right;
}
else
{
return left;
}
}
else
{
return -1;
}
}

/*
p-ийн зааж буй CBTree-г idx дугаартай зангилаанаас эхлэн preorder-оор
хэвлэ.
Орой бүрийг нэг шинэ мөрөнд хэвлэнэ.
*/
void cb_preorder(const CBTree *p, int idx)
{
/* Энд pre-order-оор хэвлэх үйлдлийг хийнэ үү */
if (idx < p->cb_len)
{
printf("%d\n", p->cb_arr[idx]);
cb_preorder(p, idx * 2 + 1);
cb_preorder(p, idx * 2 + 2);
}
}

/*
p-ийн зааж буй CBTree-г idx дугаартай зангилаанаас эхлэн in-order-оор
хэвлэ.
Орой бүрийг нэг шинэ мөрөнд хэвлэнэ.
*/
void cb_inorder(const CBTree *p, int idx)
{
/* Энд in-order-оор хэвлэх үйлдлийг хийнэ үү */
if (idx < p->cb_len)
{
cb_inorder(p, idx * 2 + 1);
printf("%d\n", p->cb_arr[idx]);
cb_inorder(p, idx * 2 + 2);
}
}

/*
p-ийн зааж буй CBTree-г idx дугаартай зангилаанаас эхлэн post-order-
оор хэвлэ.
Орой бүрийг нэг шинэ мөрөнд хэвлэнэ.
*/
void cb_postorder(const CBTree *p, int idx)
{
/* Энд post-order-оор хэвлэх үйлдлийг хийнэ үү */
if (idx < p->cb_len)
{
cb_postorder(p, idx * 2 + 1);

cb_postorder(p, idx * 2 + 2);


printf("%d\n", p->cb_arr[idx]);
}
}

/*
p-ийн зааж буй CBTree-с idx дугаартай зангилаанаас доошхи бүх навчийг
олно.
Навч тус бүрийн утгыг шинэ мөрөнд хэвлэнэ.
Навчыг зүүнээс баруун тийш олдох дарааллаар хэвлэнэ.
*/
void cb_leaves(const CBTree *p, int idx)
{
/* Энд навчуудыг үйлдлийг хийнэ үү */
if (idx * 2 + 1 < p->cb_len)
{
cb_leaves(p, idx * 2 + 1);
if (idx * 2 + 2 < p->cb_len)
{
cb_leaves(p, idx * 2 + 2);
}
}
else
{
printf("%d\n", p->cb_arr[idx]);
}
}

/*
p-ийн зааж буй CBTree-д idx индекстэй оройноос доошхи бүх үр садыг
хэвлэнэ.
Тухайн орой өөрөө үр сад болохгүй.
Үр, сад бүрийг нэг шинэ мөрөнд хэвлэнэ. Үр садыг pre-order дарааллаар
хэлэх ёстой.
*/
void cb_descendants(const CBTree *p, int idx)
{
/* Энд үр садыг олох үйлдлийг хийнэ үү */

cb_preorder(p, idx * 2 + 1);


cb_preorder(p, idx * 2 + 2);
}

/*
p-ийн зааж буй Tree-д хэдэн элемент байгааг буцаана.
CBTree-д өөрчлөлт оруулахгүй.
*/
int cb_size(const CBTree *p)
{
/* Энд хэмжээг олох үйлдлийг хийнэ үү */
return p->cb_len;
}

/*
p-ийн зааж буй CBTree-д x утгаас үндэс хүртэлх оройнуудын тоог
буцаана.
x тоо олдохгүй бол -1-г буцаана.
*/
int cb_level(const CBTree *p, int x)
{
/* Энд түвшинг олох үйлдлийг хийнэ үү */
int saver = -2, count = 0;
for (int i = 0; i < p->cb_len; i++)
{
if (p->cb_arr[i] == x)
{
saver = i;
break;
}
}
if (saver == -2)
{
return -1;
};
while (saver > 0)
{
saver = (saver - 1) / 2;
count++;
}
return count;
}

18B1NUM0940 G. Mungunsuvd

You might also like