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

1d Array

1. The document describes algorithms for insertion, deletion, searching, sorting, and reversing an array in C programming. It includes pseudo code for linear search, bubble sort, insertion, deletion, and reversing an array. 2. The full source code is provided to implement these algorithms on an integer array of size 10. The code includes functions for each algorithm and a main function to accept user input and display output. 3. Sample output is shown for inserting, deleting, sorting, reversing elements of the array and searching an element using the linear search algorithm.

Uploaded by

Souradeep Ghosh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

1d Array

1. The document describes algorithms for insertion, deletion, searching, sorting, and reversing an array in C programming. It includes pseudo code for linear search, bubble sort, insertion, deletion, and reversing an array. 2. The full source code is provided to implement these algorithms on an integer array of size 10. The code includes functions for each algorithm and a main function to accept user input and display output. 3. Sample output is shown for inserting, deleting, sorting, reversing elements of the array and searching an element using the linear search algorithm.

Uploaded by

Souradeep Ghosh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Date:

Assignment Number:
Problem statement:
Program in c to perform 1: insertion, 2: deletion, 3: Searching, 4: Sorting, 5:
Reverse an array
ALGORITHM:
Input: An array A[10]

Output: The item is found successfully in specific position or suitable unsuccessful massage.

DATA STRUCTURE: A global array a of size 10

LINEAR SEARCH
STEP 1: SET I TO 1

STEP 2: IF I >= N THEN GO TO STEP 7

STEP 3: IF A[I] = X THEN GO TO STEP 6

STEP 4: SET I TO I + 1

STEP 5: GO TO STEP 2

STEP 6: PRINT ELEMENT X FOUND AT INDEX I AND GO TO STEP 8

STEP 7: PRINT ELEMENT IS NOT PRESENT.

STEP 8: EXIT.

Input: An array A[10]

Output: The array is sorted successfully or suitable unsuccessful massage.

DATA STRUCTURE: A global array a of size 10

BUBBLE SORTING
STEP 1: SET P=1.

STEP 2: REPEAT THROUGH STEP 6 WHILE P<N.

STEP 3: SET J=0.

STEP 4: REPEAT THROUGH STEP 7 WHILE J<(N-P)

STEP 5: IF(A[J]>A[J+1]) THEN GOTO STEP 8.

STEP 6: SET J=J+1.

STEP 7: SET I+I+1.

Page No.
Date:

STEP 8: SET T=A[J].

STEP 9: A[J]=A[J+1].

STEP 10: SET A[J]=T.

STEP 11: END.

Input: An array A[10]

Output: The item is successfully inserted in specific position or suitable unsuccessful massage.

DATA STRUCTURE: A global array a of size 10

FOR INSERTION WITH PARAMETERS ‘ITEM’ AND ‘POS_I’


STEP 1: IF(POS_I<1)OR(POS_I>N+1) GOTO STEP 9

STEP 2: SET INDEX=POS_I-1.

STEP 3: SET K=N-1.

STEP 4: REPEAT THROUGH STEP 7 WHILE K>=INDEX.

STEP 5: SET A[K+1]=A[K].

STEP 6: SET A[INDEX]=ITEM.

STEP 7: SET K=K-1.

STEP 8: SET N=N+1.

STEP 9: PRINT “INVALID POSITION.”

STEP 10: END

Input: An array A[10]

Output: The item displayed successfully or suitable unsuccessful massage.

DATA STRUCTURE: A global array a of size 10

.DISPLAY

STEP 1: SET M=0.

STEP 2: REPEAAT THROUGH STEP 4 WHILE M<=N-1.

STEP 3: PRINT A[M].

STEP 4: SET M=M+1.

STEP 5: END

Input: An array A[10]

Output: The item is been deleted successfully from its specific position or suitable unsuccessful massage.

Page No.
Date:

DATA STRUCTURE: A global array a of size 10

.DELETE BY POSITION WITH A PARAMETER ‘POS_D’


STEP 1: SET INDEX=POS_D-1;

STEP 2: REPEAAT THROUGH STEP 5 WHILE I<=N-1.

STEP 3: SET A[I]=A[I+1]

STEP 4: SET N=N-1.

STEP 5: SET I=I+1.

STEP 6: END

Input: An array A[10]

Output: The array is successfully reversed or suitable unsuccessful massage.

DATA STRUCTURE: A global array a of size 10

REVERSE_ARRAY
STEP 1: SET M=N-1;

STEP 2: REPEAT THROUGH STEP 4 WHILE >0.

STEP 3: PRINT A[M].

STEP 4: SET M=M-1.

STEP 5: END.

Source Code:
#include<stdio.h>

#include<conio.h>

#define SIZE 10

int A[SIZE],n;

void linear_search(int element)//FUNCTION FOR SEARCH AN ELEMENT

int l=0,u=n-1;

int i;

for(i=l;i<=u;i++)

if(A[i]==element)

Page No.
Date:

printf("\n%d is found in %d position",element,i+1);

break;

if(i==u+1)

printf("\n%d is not present in the array",element);

void bubble_sort()//FUNCTION FOR SORTING AN ARRAY

int p,j,t;

for(p=1;p<n;p++)

for(j=0;j<n-p;j++)

if(A[j]>A[j+1])

t=A[j];

A[j]=A[j+1];

A[j+1]=t;

else

continue;

void insert(int pos_i,int item)

int index,k;

Page No.
Date:

if(pos_i<1||pos_i>n+1)

printf("\nInvalid position");

else

index=pos_i-1;

for(k=n-1;k>=index;k--)

A[k+1]=A[k];

A[index]=item;

n=n+1;

void display()

int m;

for(m=0;m<=n-1;m++)

printf("%d ",A[m]);

void del_pos_element(int pos_d)

int index,i;

index=pos_d-1;

for(i=index;i<n-1;i++)

A[i]=A[i+1];

n=n-1;

void reverse_array()

Page No.
Date:

int m;

for(m=n-1;m>=0;m--)

printf("%d ",A[m]);

int main()

int I ,ch, item, pos i, pos d, element;

printf("\nENTER THE NO. OF ELEMENTS OF THE ARRAY: ");

scanf("%d",&n);

if(n>SIZE-1)

printf("\nTHERE IS NO SPACE TO STORE THE ARRAY");

else

printf("\nENTER THE ELEMENTS OF THE ARRAY:\n");

for(i=0;i<n;i++)

scanf("%d",&A[i]);

printf("\nTHE ELEMENTS OF THE ARRAY IS:\n");

for(i=0;i<n;i++)

printf("%d ",A[i]);

while(1)

printf("\n1.Insert an item in a specific position in the array\n2.Delete an element from a


specific position from the array");

printf("\n3.Sorting the array\n4.Reverse the array\n5.Searching an element from the


array\n6.Exit");

printf("\nENTER YOUR CHOICE:");

scanf("%d",&ch);

switch(ch)

case 1:printf("\nEnter the item to insert:");

Page No.
Date:

scanf("%d",&item);

printf("\nEnter the position:");

scanf("%d",&pos_i);

insert(pos_i,item);

display();

break;

case 2:printf("\nEnter the position to delete:");

scanf("%d",&pos_d);

del_pos_element(pos_d);

display();

break;

case 3: bubble_sort();

printf("\nAfter sorting the array is:");

display();

break;

case 4: reverse_array();

break;

case 5:printf("\nENTER THE ELEMENTS TO SEARCH: ");

scanf("%d",&element);

linear_search(element);

break;

case 6:exit(0);

default:printf("\nWRONG CHOICE IS GIVEN");

Page No.
Date:

Output:

ENTER THE NO. OF ELEMENTS OF THE ARRAY: 5

ENTER THE ELEMENTS OF THE ARRAY:

34816

THE ELEMENTS OF THE ARRAY IS:

3 4 8 1 6

1.Insert an item in a specific position in the array

2.Delete an element from a specific position from the array

3.Sorting the array

4.Reverse the array

5.Searching an element from the array

6.Exit

ENTER YOUR CHOICE:1

Enter the item to insert:2

Enter the position:2

324816

1.Insert an item in a specific position in the array

2.Delete an element from a specific position from the array

3.Sorting the array

4.Reverse the array

5.Searching an element from the array

6.Exit

ENTER YOUR CHOICE:2

Enter the position to delete:3

32816

1.Insert an item in a specific position in the array

2.Delete an element from a specific position from the array

Page No.
Date:

3.Sorting the array

4.Reverse the array

5.Searching an element from the array

6.Exit

ENTER YOUR CHOICE:3

After sorting the array is:1 2 3 6 8

1.Insert an item in a specific position in the array

2.Delete an element from a specific position from the array

3.Sorting the array

4.Reverse the array

5.Searching an element from the array

6.Exit

ENTER YOUR CHOICE:4

86321

1.Insert an item in a specific position in the array

2.Delete an element from a specific position from the array

3.Sorting the array

4.Reverse the array

5.Searching an element from the array

6.Exit

ENTER YOUR CHOICE:5

ENTER THE ELEMENTS TO SEARCH: 6

6 is found in 4 position

1.Insert an item in a specific position in the array

2.Delete an element from a specific position from the array

3.Sorting the array

4.Reverse the array

5.Searching an element from the array

6.Exit

Page No.
Date:

Discussion:
 In array the user have to set the size of the array while compiling the program, but in linked list there is no
need for setting the size.

 Instead of using linear search we can also use binary search.

Page No.

You might also like