What is data structure ?
In computer science, a data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.
Data may be organized in many different ways the logical or mathematical model of a particular organization of data is called a data structure.
The various steps involved in sorting of an array of 5 elements given below:
Initial elements
12, 16, 3, 14, 7,
First Pass
12 16 3 14 7 no swapping
12 3 16 14 7 swapped
12 3 14 16 7 swapped
12 3 14 7 16 swapped
Second Pass
3 12 14 7 16 Swapped
3 12 14 7 16 no swapping
3 12 7 14 16 swapped
3 12 7 14 16 no swapped
Third Pass 3 12 7 14 16 no swapping
3 7 12 14 16 swapped
3 7 12 14 16 no swapping
3 7 12 14 16 no swapping
Algorithm : Bubble Sort a[n] This algorithm sorts the Array a with N element
Analysis of bubble
Sort In the Bubble sort, the first pass requires (n-1) comparisons to fix the highest element to its location, the second pass require (n-2)…, kTh pairs requires (n-k) and the last pass requires only one comparison to be fixed at its proper position. There for the total comparisons are
F(n) = (n - 1) + (n - 2) + (n - 3) +…+ (n - k) + 3 + 2 + 1 = n(n - 1)/2
F(n) = O ( n2)
In computer science, a data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.
Data may be organized in many different ways the logical or mathematical model of a particular organization of data is called a data structure.
Sorting ->
Sorting is a basic operation of computer science. Sorting refers to the operation of arranging data in some given sequence i.e., increasing or decreasing order.Bobble Sort ->
In the bubble sort, each element is compared with its adjacent element. If the first element is larger than second one then the position of the elements are interchanged otherwise it is not changed. The next element is compared with its adjacent element and the same process is repeated for all the elements in the array during the next pass the same process is the largest leaving the largest element. Suring the pass the second largest element occupies the second last position. During the next pass the same process is repeated leaving the largest element. The same process is repeated until no more elements are left for comparison. Finally the array is sorted one.The various steps involved in sorting of an array of 5 elements given below:
Initial elements
12, 16, 3, 14, 7,
First Pass
12 16 3 14 7 no swapping
12 3 16 14 7 swapped
12 3 14 16 7 swapped
12 3 14 7 16 swapped
Second Pass
3 12 14 7 16 Swapped
3 12 14 7 16 no swapping
3 12 7 14 16 swapped
3 12 7 14 16 no swapped
Third Pass 3 12 7 14 16 no swapping
3 7 12 14 16 swapped
3 7 12 14 16 no swapping
3 7 12 14 16 no swapping
Algorithm : Bubble Sort a[n] This algorithm sorts the Array a with N element
Initialization Set
I=0
Repeat steps 3 to 5 until I
Set J=0
Repeat step 5 until j
If a[J]>a[J+1] then
Set temp = a[J]
Set a[J] = a[J + 1]
Set a[J+1] = temp
End if
6. Exit
Function is C language for Bubble Sort.
void bubble_sort(int a[], int n)
{
int temp, i, j;
for( i = 0 ; i < n; i++)
{
for( j = 0; j < n -i -1; j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
}
Analysis of bubble
Sort In the Bubble sort, the first pass requires (n-1) comparisons to fix the highest element to its location, the second pass require (n-2)…, kTh pairs requires (n-k) and the last pass requires only one comparison to be fixed at its proper position. There for the total comparisons are
F(n) = (n - 1) + (n - 2) + (n - 3) +…+ (n - k) + 3 + 2 + 1 = n(n - 1)/2
F(n) = O ( n2)
Selection Sort
The selection sort technique is based upon the extension of the minimum / maximum technique. By means of a nest of for loops, a pass through the array is made to locate the minimum value. Once this found, it is placed in the first position of the array. Another position and so on. Once the next to last element is compared with the last element, all the elements have been sorted into ascending order:
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
F(n) = (n - 1) + (n - 2) + (n - 3) +…+ (n - k) + 3 + 2 + 1
= n(n - 1)/2
= O ( n2)
Insertion Sort
An insertion sort is one that sorts a set of values by inserting values into an existing sorted file.Suppose an array a with n elements a[1], a[2],….,a[n] is in memory. The insertion sort algorithms scans a from a[1] to a[n],inserting each element a[k] into its proper position in the previously sorted sub array a[1],a[2],….,a[k-1].That is: Pass 1. a[1] by itself is trivially sorted. Pass 2. a[2] is inserted either before or after a[1] so that: a[1],a[2] is sorted. Pass 3. a[3] is inserted into its proper place in a[1], a[2], that is, before a[1], between a[1] and a[2],or after a[2],so that: a[1], a[2], a[3] is sorted. Pass 4. a[4] is inserted into its proper place in a[1], a[2], a[3] so that: A[1], a[2], a[3], a[4] is sorted. ………………………………………………………………………………………………………………………………………………. Pass N. a[n] is inserted into its proper place in a[1], a[2],……, a[n-1] so that: a[1],a[2],….,a[n] is sorted. To illustrate the insertion sort method, consider the following array a with 7 elements: 25, 15, 30, 9, 99, 20, 26 Array a of 7 elements
Quick Sort
MERGE SORT
Merge sort is a sorting technique which divides the array into subarrays pf size 2 and merge adjacent Pairs. We then have approximately n/2 arrays of size 2. This process is repeated until there is only One only one array remaining of size n. Suppose an array a with n element [1], a[2],…,a[N] is in memory. The merge-sort algorithm which sorts a will first be described by means of specific example. Example: Suppose the array contains 12 element as follows: 85,76,46,92,30,41,12,19,93,3,50,11 Each pass of the merge sort algorithm will start at the beginning of the array A and merge pairs of sorted sub arrays as follow: Pass 1: Merge each pair of element to obtain the following list of sorted pairs: 76,85 46,92 30.41 12,19 3,93 11,50 Pass2: Merge each pair of obtain the lists of sorted elements. 46,76,85,92 12,19,30,41 3.11,50,93 Pass3: Again Merge the two subarrays to get two lists. 12, 19,30,41,46,76,85,92, 3,11,50,93 Pass4: Merging the above lists, we get 3,11,12,19,30,41,46,50,76,85,,92,93 In pass 4, we get sorted list of array with the size of 12 |
No comments:
Post a Comment