SlideShare une entreprise Scribd logo
1  sur  2
Insert Sorted ( ):
Description: Here A is a sorted linear array (in ascending order) with N elements.
ITEM is the value to be inserted

1. Set I = N

[Initialize counter]

2. Repeat While(ITEM < A[I]) and (I >= 1)
3.

Set A[I+1] = A[I] [Move elements downward]

4.

Set I = I – 1

void insert_sorted()
{
int i = n-1;
while(item<a[i] && i>=1)
{
a[i+1] = a[i];
i--;
}

[Decrease counter by 1]

[End of While Loop]
5. Set A[I+1] = ITEM
6. Set N = N + 1

[Insert element]

a[i+1] = item;
n++;
}

[Reset N]

7. Exit

Explanation: Here A is a sorted array stored in memory. This algorithm inserts a
data element ITEM into the (I + 1)th position in an array A. I is initialized from N i.e.
from total number of elements. ITEM is compared with each element until it finds an
element which is smaller than A[I] or it reaches the first element. During this process,
the elements are moved downwards and I is decremented. When it finds an element
smaller then ITEM, it inserts it in the next location i.e. I + 1 because I will be one
position less where ITEM is to be inserted. And finally, total number of elements is
increased by 1.

Insert Unsorted ( ):
Description: Here A is a sorted linear array with N elements. LOC is the
location where ITEM is to be inserted.
void insert_unsorted()
{
1. Set I = N
[Initialize counter]
int i=n-1;
while(i>=loc-1)
2. Repeat While (I >= LOC)
{
3.

Set A[I+1] = A[I]

4. Set I = I – 1
[End of While Loop]

[Move elements downward]
[Decrease counter by 1]

5. Set A[LOC] = ITEM
6. Set N = N + 1
7. Exit

[Insert element]
[Reset N]

a[i+1] = a[i];
i--;
}
a[loc-1] = item;
n++;
}
Explanation: Here A is an unsorted array stored in memory with N elements. This
algorithm inserts a data element ITEM into the loc th position in an array A. The
first four steps create space in A by moving downward the elements of A. These
elements are moved in reverse order i.e. first A[N], then A[N–1], A[N–2]. . . . . and last
A[LOC], otherwise data will be overwritten. We first set I=N and then, using Ias a
counter, decrease it each time the loop is executed until I reaches LOC. In the next
step, Step 5, it inserts ITEM into the array in the space just created. And at last, th
e total number of elements N is increased by 1.
Delete ( ):
Description: Here A is a linear array with N elements. LOC is the location
from where ITEM is to be deleted..
1. Set ITEM = A[LOC] [Assign the element to be deleted to ITEM]

item = a[loc-1];

2. Repeat For I = LOC to N

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

3. Set A[I] = A[I+1]
[End of For Loop]

a[i] = a[i+1];

4. Set N = N – 1
5. Exit

[Move the Ith element upwards]

[Reset N]

n--;
printf("nITEM deleted:
%d", item);
}

Explanation: First, the element to be deleted is assigned to ITEM from location A
[LOC]. Then I is set to LOC from where ITEM is to be deleted and it iterated to total
number of elements i.e. N. During this loop, the elements are moved upwards. And lastly,
total number of elements is decreased by 1

Contenu connexe

Tendances (20)

Sorting
SortingSorting
Sorting
 
Arrays Data Structure
Arrays Data StructureArrays Data Structure
Arrays Data Structure
 
Queue
QueueQueue
Queue
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory Mapping
 
Heaps & Adaptable priority Queues
Heaps & Adaptable priority QueuesHeaps & Adaptable priority Queues
Heaps & Adaptable priority Queues
 
Sienna 8 countingsorts
Sienna 8 countingsortsSienna 8 countingsorts
Sienna 8 countingsorts
 
Queue
QueueQueue
Queue
 
4. Queues in Data Structure
4. Queues in Data Structure4. Queues in Data Structure
4. Queues in Data Structure
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Queue
QueueQueue
Queue
 
two dimensional array
two dimensional array two dimensional array
two dimensional array
 
Circular queue
Circular queueCircular queue
Circular queue
 
Notes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queueNotes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queue
 
Insertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexityInsertion Sort, Quick Sort And Their complexity
Insertion Sort, Quick Sort And Their complexity
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
 
Arrays in Data
Arrays in DataArrays in Data
Arrays in Data
 
Object-Oriented Programming in Functional Programming in Swift
Object-Oriented Programming in Functional Programming in SwiftObject-Oriented Programming in Functional Programming in Swift
Object-Oriented Programming in Functional Programming in Swift
 
Counting Sort Lowerbound
Counting Sort LowerboundCounting Sort Lowerbound
Counting Sort Lowerbound
 

En vedette

Dual Linear Array Probe for Corrosion Imaging
Dual Linear Array Probe for Corrosion ImagingDual Linear Array Probe for Corrosion Imaging
Dual Linear Array Probe for Corrosion ImagingOlympus IMS
 
Design of Linear Array Transducer Using Ultrasound Simulation Program Field-II
Design of Linear Array Transducer Using Ultrasound Simulation Program Field-IIDesign of Linear Array Transducer Using Ultrasound Simulation Program Field-II
Design of Linear Array Transducer Using Ultrasound Simulation Program Field-IIinventy
 
Preparation Data Structures 04 array linear_list
Preparation Data Structures 04 array linear_listPreparation Data Structures 04 array linear_list
Preparation Data Structures 04 array linear_listAndres Mendez-Vazquez
 
Phased array antenna
Phased array antennaPhased array antenna
Phased array antennaShaveta Banda
 

En vedette (7)

Dual Linear Array Probe for Corrosion Imaging
Dual Linear Array Probe for Corrosion ImagingDual Linear Array Probe for Corrosion Imaging
Dual Linear Array Probe for Corrosion Imaging
 
Design of Linear Array Transducer Using Ultrasound Simulation Program Field-II
Design of Linear Array Transducer Using Ultrasound Simulation Program Field-IIDesign of Linear Array Transducer Using Ultrasound Simulation Program Field-II
Design of Linear Array Transducer Using Ultrasound Simulation Program Field-II
 
DATASTRUCTURES UNIT-1
DATASTRUCTURES UNIT-1DATASTRUCTURES UNIT-1
DATASTRUCTURES UNIT-1
 
Preparation Data Structures 04 array linear_list
Preparation Data Structures 04 array linear_listPreparation Data Structures 04 array linear_list
Preparation Data Structures 04 array linear_list
 
array and phased array antennna
array and phased array antennnaarray and phased array antennna
array and phased array antennna
 
Phased array antenna
Phased array antennaPhased array antenna
Phased array antenna
 
Array antennas
Array antennasArray antennas
Array antennas
 

Similaire à A sorted linear array

Array data structure
Array data structureArray data structure
Array data structureharsh112327
 
هياكلبيانات
هياكلبياناتهياكلبيانات
هياكلبياناتRafal Edward
 
Array data structure
Array data structureArray data structure
Array data structuremaamir farooq
 
Computer Engineering Data Structure Queue.pptx
Computer Engineering Data Structure Queue.pptxComputer Engineering Data Structure Queue.pptx
Computer Engineering Data Structure Queue.pptxNingthoujamMahesh1
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptxchouguleamruta24
 
ARRAY in python and c with examples .pptx
ARRAY  in python and c with examples .pptxARRAY  in python and c with examples .pptx
ARRAY in python and c with examples .pptxabhishekmaurya102515
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortsearch_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortShanmuganathan C
 
Revisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queueRevisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queuessuser7319f8
 
Updated Lab3.docx
Updated Lab3.docxUpdated Lab3.docx
Updated Lab3.docxAleezaAnjum
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sortingKaushal Shah
 

Similaire à A sorted linear array (20)

sorting1.pptx
sorting1.pptxsorting1.pptx
sorting1.pptx
 
Queues.ppt
Queues.pptQueues.ppt
Queues.ppt
 
Queue
QueueQueue
Queue
 
Array data structure
Array data structureArray data structure
Array data structure
 
DSA - Array.pptx
DSA - Array.pptxDSA - Array.pptx
DSA - Array.pptx
 
هياكلبيانات
هياكلبياناتهياكلبيانات
هياكلبيانات
 
Queue
QueueQueue
Queue
 
Array data structure
Array data structureArray data structure
Array data structure
 
Computer Engineering Data Structure Queue.pptx
Computer Engineering Data Structure Queue.pptxComputer Engineering Data Structure Queue.pptx
Computer Engineering Data Structure Queue.pptx
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
 
ARRAY in python and c with examples .pptx
ARRAY  in python and c with examples .pptxARRAY  in python and c with examples .pptx
ARRAY in python and c with examples .pptx
 
Unit 7 sorting
Unit 7   sortingUnit 7   sorting
Unit 7 sorting
 
Arrays.pptx
Arrays.pptxArrays.pptx
Arrays.pptx
 
Data structures
Data structuresData structures
Data structures
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortsearch_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sort
 
Queues
QueuesQueues
Queues
 
arrays
arraysarrays
arrays
 
Revisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queueRevisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queue
 
Updated Lab3.docx
Updated Lab3.docxUpdated Lab3.docx
Updated Lab3.docx
 
Data Structures - Searching & sorting
Data Structures - Searching & sortingData Structures - Searching & sorting
Data Structures - Searching & sorting
 

Plus de Suneel Dogra

Distributed databases
Distributed databasesDistributed databases
Distributed databasesSuneel Dogra
 
Data base management system
Data base management systemData base management system
Data base management systemSuneel Dogra
 
Web sitedesignpart1
Web sitedesignpart1Web sitedesignpart1
Web sitedesignpart1Suneel Dogra
 
Web sitedesignpart1
Web sitedesignpart1Web sitedesignpart1
Web sitedesignpart1Suneel Dogra
 
He 12 different types of servers that every techie should know about
He 12 different types of servers that every techie should know aboutHe 12 different types of servers that every techie should know about
He 12 different types of servers that every techie should know aboutSuneel Dogra
 
Bachelor of computer application b.c.a.-2014
Bachelor of computer application b.c.a.-2014Bachelor of computer application b.c.a.-2014
Bachelor of computer application b.c.a.-2014Suneel Dogra
 
Cloud computing application
Cloud computing applicationCloud computing application
Cloud computing applicationSuneel Dogra
 
Fast track to linux
Fast track to linuxFast track to linux
Fast track to linuxSuneel Dogra
 
Jumping statements
Jumping statementsJumping statements
Jumping statementsSuneel Dogra
 

Plus de Suneel Dogra (20)

Business model
Business modelBusiness model
Business model
 
Internet
InternetInternet
Internet
 
Html
HtmlHtml
Html
 
Dreamweaver
DreamweaverDreamweaver
Dreamweaver
 
Advanced html
Advanced htmlAdvanced html
Advanced html
 
Sql
SqlSql
Sql
 
File organisation
File organisationFile organisation
File organisation
 
Distributed databases
Distributed databasesDistributed databases
Distributed databases
 
Database models
Database models Database models
Database models
 
Data base management system
Data base management systemData base management system
Data base management system
 
Web sitedesignpart1
Web sitedesignpart1Web sitedesignpart1
Web sitedesignpart1
 
Web sitedesignpart1
Web sitedesignpart1Web sitedesignpart1
Web sitedesignpart1
 
Internet security
Internet securityInternet security
Internet security
 
What is the linux
What is the linuxWhat is the linux
What is the linux
 
He 12 different types of servers that every techie should know about
He 12 different types of servers that every techie should know aboutHe 12 different types of servers that every techie should know about
He 12 different types of servers that every techie should know about
 
Bachelor of computer application b.c.a.-2014
Bachelor of computer application b.c.a.-2014Bachelor of computer application b.c.a.-2014
Bachelor of computer application b.c.a.-2014
 
Cloud computing application
Cloud computing applicationCloud computing application
Cloud computing application
 
Fast track to linux
Fast track to linuxFast track to linux
Fast track to linux
 
String in c
String in cString in c
String in c
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
 

A sorted linear array

  • 1. Insert Sorted ( ): Description: Here A is a sorted linear array (in ascending order) with N elements. ITEM is the value to be inserted 1. Set I = N [Initialize counter] 2. Repeat While(ITEM < A[I]) and (I >= 1) 3. Set A[I+1] = A[I] [Move elements downward] 4. Set I = I – 1 void insert_sorted() { int i = n-1; while(item<a[i] && i>=1) { a[i+1] = a[i]; i--; } [Decrease counter by 1] [End of While Loop] 5. Set A[I+1] = ITEM 6. Set N = N + 1 [Insert element] a[i+1] = item; n++; } [Reset N] 7. Exit Explanation: Here A is a sorted array stored in memory. This algorithm inserts a data element ITEM into the (I + 1)th position in an array A. I is initialized from N i.e. from total number of elements. ITEM is compared with each element until it finds an element which is smaller than A[I] or it reaches the first element. During this process, the elements are moved downwards and I is decremented. When it finds an element smaller then ITEM, it inserts it in the next location i.e. I + 1 because I will be one position less where ITEM is to be inserted. And finally, total number of elements is increased by 1. Insert Unsorted ( ): Description: Here A is a sorted linear array with N elements. LOC is the location where ITEM is to be inserted. void insert_unsorted() { 1. Set I = N [Initialize counter] int i=n-1; while(i>=loc-1) 2. Repeat While (I >= LOC) { 3. Set A[I+1] = A[I] 4. Set I = I – 1 [End of While Loop] [Move elements downward] [Decrease counter by 1] 5. Set A[LOC] = ITEM 6. Set N = N + 1 7. Exit [Insert element] [Reset N] a[i+1] = a[i]; i--; } a[loc-1] = item; n++; }
  • 2. Explanation: Here A is an unsorted array stored in memory with N elements. This algorithm inserts a data element ITEM into the loc th position in an array A. The first four steps create space in A by moving downward the elements of A. These elements are moved in reverse order i.e. first A[N], then A[N–1], A[N–2]. . . . . and last A[LOC], otherwise data will be overwritten. We first set I=N and then, using Ias a counter, decrease it each time the loop is executed until I reaches LOC. In the next step, Step 5, it inserts ITEM into the array in the space just created. And at last, th e total number of elements N is increased by 1. Delete ( ): Description: Here A is a linear array with N elements. LOC is the location from where ITEM is to be deleted.. 1. Set ITEM = A[LOC] [Assign the element to be deleted to ITEM] item = a[loc-1]; 2. Repeat For I = LOC to N for(i=loc-1; i<n; i++) 3. Set A[I] = A[I+1] [End of For Loop] a[i] = a[i+1]; 4. Set N = N – 1 5. Exit [Move the Ith element upwards] [Reset N] n--; printf("nITEM deleted: %d", item); } Explanation: First, the element to be deleted is assigned to ITEM from location A [LOC]. Then I is set to LOC from where ITEM is to be deleted and it iterated to total number of elements i.e. N. During this loop, the elements are moved upwards. And lastly, total number of elements is decreased by 1